Data is passed to wrong array element

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2009
Posts: 2
Reputation: Triztian is an unknown quantity at this point 
Solved Threads: 0
Triztian Triztian is offline Offline
Newbie Poster

Data is passed to wrong array element

 
0
  #1
Oct 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso
 
0
  #2
Oct 14th, 2009
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:
  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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 2
Reputation: Triztian is an unknown quantity at this point 
Solved Threads: 0
Triztian Triztian is offline Offline
Newbie Poster
 
0
  #3
Oct 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 2
Reputation: matome is an unknown quantity at this point 
Solved Threads: 0
matome matome is offline Offline
Newbie Poster
 
0
  #4
Oct 14th, 2009
because arrays will alwayas allocate the the first index of the array to 0,
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 33
Reputation: SVR is an unknown quantity at this point 
Solved Threads: 4
SVR SVR is offline Offline
Light Poster
 
0
  #5
Oct 14th, 2009
Originally Posted by vmanes View Post
Typical loop usage:
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso
 
0
  #6
Oct 14th, 2009
YES, that was a typo
Should read
for( c = 0; c < 5; c++ )

(walks away with embarrassed look)
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC