Why won't this array work?

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
This program will output the graph of y = x 2. Ask the user for at least 4 integer
inputs for the x-coordinate. The inputted x-coordinate values are to be stored in
an array named xCoord.
Write a user-defined function that will compute the value of the y-coordinate
given the x-coordinate value. The computed y-coordinate values are to be stored
in an array named yCoord.
Function prototype: int computeSquare(int x);
Your program will then plot each of the coordinates. (If you do not wish to graph,
then simply output a table of values in an organized manner.)


So I'm trying to get the user to input 4 coordinates, I figured that, so now to get the y-coordinates I made it

yCoord = xCoord * xCoord to create an array of the four numbers entered in the xCoord array, then I tried yCoord = (xCoord)^2, no go...

The result I get:

1 2 3 4
1245052

Why does it come out like this? Heres the code.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Well, first off "^" in C is "bitwise exclusive or", not "exponent". You should go back to "xCoord[ i ] * xCoord[ i ]"

Your second problem is that you created arrays of int values, and then tried to read in double values from scanf. If you want to use doubles, create arrays of doubles.

Third, if you want to make a for loop apply to more than one line of code, you need to put braces around it. So, if you want to make your printf statement run 4 times, you need to stick it inside your for loop, like this:
for(i = 0; i < 4; ++i){
yCoord[ i ] = xCoord[ i ] * xCoord[ i ];
printf("%d\n", &yCoord[ i ]);
}

Also, don't put semicolons at the end of a "for" statement like you have.

And finally, if you are using double values instead of ints, then when you do printf, use "%lf" like you dod when you read in the values. "%d" prints integers, "%lf" prints doubles.
 

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
Originally posted by: notfred
Well, first off "^" in C is "bitwise exclusive or", not "exponent". You should go back to "xCoord[ i ] * xCoord[ i ]"

Your second problem is that you created arrays of int values, and then tried to read in double values from scanf. If you want to use doubles, create arrays of doubles.

Third, if you want to make a for loop apply to more than one line of code, you need to put braces around it. So, if you want to make your printf statement run 4 times, you need to stick it inside your for loop, like this:
for(i = 0; i < 4; ++i){
yCoord[ i ] = xCoord[ i ] * xCoord[ i ];
printf("%d\n", &yCoord[ i ]);
}

Also, don't put semicolons at the end of a "for" statement like you have.

And finally, if you are using double values instead of ints, then when you do printf, use "%lf" like you dod when you read in the values. "%d" prints integers, "%lf" prints doubles.

Well I tried fixing it like you said, still no go, I get the same result.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
oh, one more problem. printf takes an actual number, bnot a reference, so remove the "&" in front of "yCoord" in the printf statement. Also note that I didn't change the"%d" to "%lf" in the code I copy-pasted from you, but I told you to do that. Make sure you're actually reading what I said and not just copying and pasting my code.
 

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
Originally posted by: notfred
oh, one more problem. printf takes an actual number, bnot a reference, so remove the "&" in front of "yCoord" in the printf statement. Also note that I didn't change the"%d" to "%lf" in the code I copy-pasted from you, but I told you to do that. Make sure you're actually reading what I said and not just copying and pasting my code.

sweet, she works, but I kept them all as ints, double made it messy. Anyways one last question before I go back to my first problem.

When I input x-coordinats as 1 2 3 4 with a space, the results come out as 14916, which are correct, but how do I put a space between each value?

thanks :)
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
In the printf statement, where you have "%d\n", just put a space after the "%d" so that you get "%d \n".
 

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
Originally posted by: notfred
In the printf statement, where you have "%d\n", just put a space after the "%d" so that you get "%d \n".

Thanks working perfecty now :).
 

Gunslinger08

Lifer
Nov 18, 2001
13,234
2
81
Also, in your for loops, you loop up to 4, when you should be looping up to coordx and coordy. Otherwise the work you did in defining them is pointless, because you still have to change them in more than one spot.