• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Why won't this array work?

mOeeOm

Platinum Member
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.
 
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.
 
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.
 
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.
 
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 🙂
 
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 🙂.
 
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.
 
Back
Top