944,052 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 376
  • C++ RSS
Oct 14th, 2009
0

Data is passed to wrong array element

Expand Post »
Hi I think this is my first post, so here it goes:
Overview:

This program declares global bidimensional array of 30 rows and 5 columns (not counting 0 in the rows) of floating type to save information of students, table is as follows:

ID Partial1 Partial2 Partial3 Partial4 Average

ID: em..I
Partial N: Students grade of a evaluation.
Average: well students final grade.

the grade can't be higher than 10.
Problem:

Ok so my problem is that when I start inputing data everything goes well until we input more than 1 student, when I enter the second ID it is saved but the second student's ID overwrites the last student's average, having the ID duplicated and the las average erased, and i can't figure out why is this happening, I've looked at the code for hours, so any help is really appreciated, by the way I'm relatively new to c++.
Notes:
  1. //Start of data input
  2. for (row= 1;row<=30;row++){
  3. Sum=0;//reset acumulator.
  4.  
  5. printf("\nStudent %d.", row);
  6. printf("\n\tAverage: ");scanf("%f",&alumno[row][0]);
  7.  
  8. for (col=1;col<=4;col++){
  9. printf("\n\tPartial %d: ",col);scanf("%f",&alumno[row][col]);
  10. Sum=Sum+alumno[row][col];//acumulating values for average
  11. }
  12.  
  13. alumno[row][5]=Sum/4; //this is the average
  14. printf("\nAverage: %0.2f", alumno[row][5]);
  15. }//end of input
Output

  1. ID P1 P2 P3 P4 Av
  2. 10 10 10 10 10 9
  3. 9 9 9 9 9 6
  4. 6 6 6 6 6 6

Data of 3 students
assuming you use 10 for all of the first student's data, 9 for the second and 6 for the third.

see how the last value of the first and second student gets replaced by the ID of the next one.
Similar Threads
Reputation Points: 10
Solved Threads: 4
Light Poster
Triztian is offline Offline
28 posts
since Sep 2009
Oct 14th, 2009
0
Re: Data is passed to wrong array element
Is your array declared as 5 columns or as 6? It makes a difference in the indexing you use.
Are you using the row index as the student ID? I don't see input for IDs.

Remember that the maximum index you can (safely) use is one less than the size of the array dimension. So, if your array is [30][5], your maximum index is [29][4]. By putting the average in column index [5], you are probably overwriting the first position of the next row.

Typical loop usage:
C++ Syntax (Toggle Plain Text)
  1. int arr[10][5];
  2. int r, c;
  3. for( r = 0; i < 10; r++ )
  4. for( c = 0; c < 6; c++ )
  5. //do something with arr[r][c]
By not using index 0, you waste a row and a column, and your looping takes a bit more care to understand and do correctly.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 14th, 2009
0
Re: Data is passed to wrong array element
het thanks for ypur answer, redimensioning tn columns to 6 solved the issue, Just on last question, why does it overwrite?, I mean why is it safe to use cols-1 as the last index.

Thread Solved.
Reputation Points: 10
Solved Threads: 4
Light Poster
Triztian is offline Offline
28 posts
since Sep 2009
Oct 14th, 2009
0
Re: Data is passed to wrong array element
because arrays will alwayas allocate the the first index of the array to 0,
Reputation Points: 10
Solved Threads: 0
Newbie Poster
matome is offline Offline
2 posts
since Sep 2007
Oct 14th, 2009
0
Re: Data is passed to wrong array element
Click to Expand / Collapse  Quote originally posted by vmanes ...
Typical loop usage:
C++ Syntax (Toggle Plain Text)
  1. int arr[10][5];
  2. int r, c;
  3. for( r = 0; i < 10; r++ )
  4. for( c = 0; c < 6; c++ ) // <--- typo ?
  5. //do something with arr[r][c]
I'm assuming that was a typo?

@Triztian:

Array elements follow each other contiguously in memory.

int arr[2][5] will look like this in memory

0,0 | 0,1 | 0,2 | 0,3 | 0,4 | 1,0 | 1,1 | 1,2 | 1,3 | 1,4

So if you write to 0,5 it goes into the slot for 1,0.

Always use array indexing from 0 to n-1.
SVR
Reputation Points: 10
Solved Threads: 4
Light Poster
SVR is offline Offline
44 posts
since May 2008
Oct 14th, 2009
0
Re: Data is passed to wrong array element
YES, that was a typo
Should read
for( c = 0; c < 5; c++ )

(walks away with embarrassed look)
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: operator overloading to add matrix
Next Thread in C++ Forum Timeline: Comparing list iterators





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC