Computer Science Canada Little array issue |
Author: | Ninja [ Thu Mar 13, 2008 5:56 pm ] | ||
Post subject: | Little array issue | ||
Hey guys!.Ok so i was working on this assignment.Basically here is what it is.An array holds marks entered by the user.We have to assume that the user wont enter more than 40 values. So i wrote the code for it (sorry for any sloppyness or errors, still working on it lol).The user can terminate the program by entering -1..but when you go to print the array (say you enter 4 marks) it prints all 40 instead of just the 4 entered.Here is the code...can anyone tell me what i'm doing wrong here cause i can't figure out.Thanks in advance!
|
Author: | HeavenAgain [ Thu Mar 13, 2008 6:00 pm ] | ||||
Post subject: | RE:Little array issue | ||||
|
Author: | Saad [ Thu Mar 13, 2008 6:01 pm ] | ||
Post subject: | Re: Little array issue | ||
You use MAX as your number of scores entered when printing them out Hoever you should be marks_size that you pass to the printArray function. Saad |
Author: | Ninja [ Thu Mar 13, 2008 7:02 pm ] | ||||
Post subject: | Re: RE:Little array issue | ||||
HeavenAgain @ Thu Mar 13, 2008 6:00 pm wrote:
Hmmm...but the array is still initialized with a size of 40..any way to change the size of the array by counting how many valid numbers the user enters.Ahhh..i wish i could use C++ for this class, vectors make life easier lol. |
Author: | HeavenAgain [ Thu Mar 13, 2008 7:12 pm ] | ||
Post subject: | RE:Little array issue | ||
make your valid variable global, and then you'll have
eg. your array size is 40, currently is empty with 0s now i entered 1, 2, 3, -1, 4, 5 i entered 6 numbers, valid now is 5, and i only stored 1, 2, 3, 4, 5 (with respect to their index) and now i want to print that number, so we use valid, which is 5, and print. edit: as Saad pointed out, your parameter, you passed in "printArray(marks_valid,MAX); " MAX, but instead maybe you should pass in valid, and then you dont have to make valid a global variable. |
Author: | Ninja [ Thu Mar 13, 2008 7:37 pm ] |
Post subject: | Re: Little array issue |
OMG haha..so get this.I did what saad said, but i typed it wrong and it gave me an error..so out of frustration i was like to hell with this! lol. But i got it working now, actually i just thought of what u said and it works ![]() Now i can go email this to my class cause they have no clue what to do lol. |
Author: | A.J [ Thu Mar 13, 2008 7:39 pm ] |
Post subject: | Re: Little array issue |
As HeavenAgain said, you could use a counter |