| | |
Data is passed to wrong array element
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2009
Posts: 2
Reputation:
Solved Threads: 0
Hi I think this is my first post, so here it goes:
Overview:
Output
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.
Overview:
Problem:
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.
Notes:
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++.
c Syntax (Toggle Plain Text)
//Start of data input for (row= 1;row<=30;row++){ Sum=0;//reset acumulator. printf("\nStudent %d.", row); printf("\n\tAverage: ");scanf("%f",&alumno[row][0]); for (col=1;col<=4;col++){ printf("\n\tPartial %d: ",col);scanf("%f",&alumno[row][col]); Sum=Sum+alumno[row][col];//acumulating values for average } alumno[row][5]=Sum/4; //this is the average printf("\nAverage: %0.2f", alumno[row][5]); }//end of input
c Syntax (Toggle Plain Text)
ID P1 P2 P3 P4 Av 10 10 10 10 10 9 9 9 9 9 9 6 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.
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:
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.
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)
int arr[10][5]; int r, c; for( r = 0; i < 10; r++ ) for( c = 0; c < 6; c++ ) //do something with arr[r][c]
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 4
0
#5 Oct 14th, 2009
•
•
•
•
Typical loop usage:
C++ Syntax (Toggle Plain Text)
int arr[10][5]; int r, c; for( r = 0; i < 10; r++ ) for( c = 0; c < 6; c++ ) // <--- typo ? //do something with arr[r][c]
@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.
0
#6 Oct 14th, 2009
YES, that was a typo
Should read
for( c = 0; c < 5; c++ )
(walks away with embarrassed look)
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- delete same array element in C (C)
- Completely deleting a string array element : (C++)
- returning a array element to the main function (Java)
- Passing Array element to a list (Java)
- Delete or Remove Array Element (C++)
- getting a variable name (Python)
- how to read data from textfile and push in array (C++)
- keep getting "unitialized value in array element" (Perl)
- Array of pointers (C++)
Other Threads in the C++ Forum
- Previous Thread: operator overloading to add matrix
- Next Thread: Comparing list iterators
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






