Expected expression error - Objective c - objective-c

I'm doing a tutorial from this book: "Objective-C 2.0 Essentials 3rd edition" by Neil Smyth. I have tried repeatedly but keep getting the same "Expected expression" error even though the books version claims to run. I've checked way too many times and my version is exactly the same as the books. Please, can someone help me. Code below:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
#autoreleasepool {
int x;
int j = 54321;
for (x = 0; x < 10; x++) {
}
int j = x + 10;
NSLog (#"Variable j in for loop is %i", j);
NSLog (#"Variable j outside for loop is %i", j); /* I GET AN ERROR STATING " EXPECTED EXPRESSION HERE*/
}
return 0;
}

The line
NSLog (#"Variable j outside for loop is %i", j);
contains a lot of invisible characters (UTF-8 sequence EF BF BC = OBJECT REPLACEMENT CHARACTER) between the Tab and the "NSLog".
Deleting and rewriting that line should help.
OP's code opened in hexa editor:

Format your code better; if you do the misplaced } in the code becomes obvious:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
#autoreleasepool {
int x;
int j = 54321;
for (x = 0; x < 10; x++) {
int j = x + 10;
NSLog (#"Variable j in for loop is %i", j);
}
 NSLog (#"Variable j outside for loop is %i", j);
}
return 0;
}
EDIT The invisible characters as pointed out by #MartinR are also an issue (that I didn't notice). So there are two errors in your code.

Related

functions calling from main method objective c

Here I need to write a function which is called from main method with integer array as a parameter please give me example.
In below example parameter are int type.
Note : please tell this is correct way to do this or not...
#import <Foundation/Foundation.h>
void displayit (int);
int main (int argc, const char * argv[])
{
#autoreleasepool {
int i;
for (i=0; i<5; i++)
{
displayit( i );
}
}
return 0;
}
void displayit (int i)
{
int y = 0;
y += i;
NSLog (#"y + i = %i", y);
}
Thanks in advance....
I tried out these, please check.
#import <Foundation/Foundation.h>
void displayit (int array[], int len);
int main (int argc, const char * argv[])
{
#autoreleasepool {
int array[]={1,2,3};
displayit( array, 3 );
}
return 0;
}
void displayit (int array[], int len)
{
for(int i=0;i<len;i++){
NSLog(#"display %d : %d",i,array[i]);
}
}
The out put is:
2014-10-30 14:09:32.017 OSTEST[32541:77397] display 0 : 1
2014-10-30 14:09:32.018 OSTEST[32541:77397] display 1 : 2
2014-10-30 14:09:32.018 OSTEST[32541:77397] display 2 : 3
Program ended with exit code: 0
I used another parameter len to avoid boundary beyond.
If the array is a global, static, or automatic variable (int array[10];), then sizeof(array)/sizeof(array[0]) works. Quoted From Another Question

Project Euler # 10 in Objective C

I'm trying to solve Problem 10 in Project Euler, and while I thought I had it, its saying my answer is incorrect. The question is as follows:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
And my code:
int sum;
#interface Prime : NSObject
-(BOOL)isPrime:(int)arg1;
#end
#implementation Prime
-(BOOL)isPrime:(int)arg1 {
if (arg1 == 1) {
NSLog(#"Given 1");
return NO;
}
for (int i = 2; i < arg1; i++) {
if (arg1 % i == 0) {
return NO;
}
}
sum += arg1;
return YES;
}
#end
int main(int argc, const char * argv[])
{
#autoreleasepool {
Prime* primeObject = [[Prime alloc] init];
for (int i = 0; i < 2000000; i++) {
[primeObject isPrime:i];
}
NSLog(#"Sum of primes is %i", sum);
}
}
This code outputs 'Sum of primes is 1179908154' which Project Euler says is incorrect. Help?
The problem is that the sum does not fit into a 32-bit integer. You should use long long instead.
Just a guess, you should try to:
Initialise the sum variable to 0.
Try not to use a global variable like sum that can be accessed from anywhere, in this case do the sum in the main loop instead of in the isPrime method.
Maybe that'll give you the right answer.
You are using int for getting result, so it is wrong.
I'm using long int instead, that is enough for this case.
Here is my code, and it works fine:
int inputNumber = 2000000;
long int result = 0;
for (int i = 2; i < inputNumber; i++) {
BOOL isPrime = YES;
for (int j = 2; j <= sqrt(i); j++) {
if (i%j==0) {
isPrime = NO;
break;
}
}
if (isPrime) {
result += i;
}
}
Result is: 142913828922

Converting NSString to array of chars inside For Loop

I'm trying to use an existing piece of code in an iOS project to alphabetize a list of words in an array (for instance, to make tomato into amoott, or stack into ackst). The code seems to work if I run it on its own, but I'm trying to integrate it into my existing app.
Each word I want it to alphabetize is stored as an NSString inside an array. The issue seems to be that the code takes the word as an array of chars, and I can't get my NSStrings into that format.
If I use string = [currentWord UTFString], I get an error of Array type char[128] is not assignable, and if I try to create the char array inside the loop (const char *string = [curentWord UTF8String]) I get warnings relating to Initializing char with type const char discards qualifiers. Not quite sure how I can get around it – any tips? The method is below, I'll take care of storing the alphabetized versions later.
- (void) alphabetizeWord {
char string[128], temp;
int n, i, j;
for (NSString* currentWord in wordsList) {
n = [currentWord length];
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (string[i] > string[j]) {
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
}
}
NSLog(#"The word %# in alphabetical order is %s", currentWord, string);
}
}
This should work :
- (void)alphabetizeWord {
char str[128];
for (NSString *currentWord in wordList)
{
int wordLength = [currentWord length];
for (int i = 0; i < wordLength; i++)
{
str[i] = [currentWord characterAtIndex:i];
}
// Adding the termination char
str[wordLength] = 0;
// Add your word
}
}
EDIT : Sorry, didn't fully understand at first. Gonna check this out.

How do I convert a Hexa-Tri-Decimal number into an int in objective c?

The Hexa-Tri-Decimal number is 0-9 and A-Z. I know I can covert from hex with a NSScanner but not sure how to go about converting Hexa-Tri-Decimal.
For example I have a NSString with "0XPM" the int value should be 43690, "1BLC" would be 61680.
Objective C is built on top of C, and luckily enough you can use the functions there to accomplish the conversion. What you're looking for is strtol or one of it's sibling functions. If I recall correctly strtol handles up to base36 (the hexa-tri-decimal you refer to).
http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/
I can only think to do this using C strings, as they offer easier access to individual characters.
This seemed like an interesting problem to solve, so I had a go at writing it:
int parseBase36Number(NSString *input)
{
const char *inputCString = [[input lowercaseString] UTF8String];
size_t inputLength = [input length];
int orderOfMagnitudeMultiplier = 1;
int result = 0;
// iterate backward through the number
for (int i = inputLength - 1; i >= 0; i--)
{
char inputChar = inputCString[i];
int charNumericValue;
if (isdigit(inputChar))
{
charNumericValue = inputChar - '0';
}
else if (islower(inputChar))
{
charNumericValue = inputChar - 'a' + 10;
}
else
{
// unhanded character, throw error
}
result += charNumericValue * orderOfMagnitudeMultiplier;
orderOfMagnitudeMultiplier *= 36;
}
return result;
}
NOTE: I've not tested this at all, so take care and let me know how it goes!

C logic error in for statement with break

Am running this C program, but instead of answering "The answer is 10", it sends back the message: "The answer is 0", even though it breaks at the right time.
Can you tell me what's wrong?
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
int i;
for(int i = 0; i < 12; i++){
printf("Checking i = %d\n", i);
if(i + 90 == i * i) {
break;
}
}
printf("The answer is %d.\n", i);
}
The problem is you have two i's.
int main (int argc, const char * argv[])
{
int i; //Declares outer i
for(int i = 0; i < 12; i++) //Declares a NEW i
{
printf("Checking i = %d\n", i);
if(i + 90 == i * i)
{
break;
}
}
printf("The answer is %d.\n", i); //Uses the outer i
}
Basic scope confusion: You have two different variables called i: One in the outer scope of the main function body, and another, overriding one inside the for loop.
The outer variable is uninitialized, so in fact you have undefined behaviour.
What you mean to say is this:
int i;
for (i = 0; i < 12; i++)
/* ^^^^^ use existing variable! */
{
printf("Checking i = %d\n", i);
if (i + 90 == i * i)
{
break;
}
}
Could it be the extra "int"? You're declaring another instance of "i" in the for loop that goes out of scope when the loop exits.
for(int i = 0; i < 12; i++){
You're creating another i here, which hides the i outside the scope of the for loop.
Change to:
int i;
...
for (i = 0; i < 12; i++){
Because you've got two DIFFERENT variables "i" - the one in the inner scope (which you increment from 0..11), and the one in the outer scope. You print the one in the outer scope.
SOLUTION:
change "for (int i=...)" to "for (i=...)"