C program help
(line) (87) #define maxStudents 45 (88) #define maxColumns 10 (89) #define maxDataPoints 271 (90) (91) void welcome (void); (92) void farewell (void); (93) int getData (int aryStudentData [][maxColumns], int maxDataPoints); (94) int doCalc (int aryStudentData [][maxColumns]); (95) int printData (int aryStudentData [][maxColumns]);
Those are the lines that are relevant; everything above line 87 is comments, except for the include < stdio.h > line. I can post the rest of the source if necessary, but I don't think it is.
The program isn't done, but I'm trying to debug it as I go. Here are the errors Visual C++.NET is giving me. I can't seem to make it happy, and it's driving me nuts.
\Documents and Settings\Kevin\My Documents\Visual Studio Projects\Lab3\BG_Lab_3.c(93) : error C2143: syntax error : missing ')' before 'constant'
\Documents and Settings\Kevin\My Documents\Visual Studio Projects\Lab3\BG_Lab_3.c(93) : error C2143: syntax error : missing '{' before 'constant'
\Documents and Settings\Kevin\My Documents\Visual Studio Projects\Lab3\BG_Lab_3.c(93) : error C2059: syntax error : '<Unknown>'
\Documents and Settings\Kevin\My Documents\Visual Studio Projects\Lab3\BG_Lab_3.c(93) : error C2059: syntax error : ')'
Help, please!! :bawling:
0
Comments
Also, though, after you do that I'd like to know if it works (not just compiles).
I think you might be given some problems because of aryStudentDate[][maxColumns]
I would just use:
int getData (int aryStudentData[][], int maxDataPoints);
int doCalc (int aryStudentData[][]);
int printData (int aryStudentData[][]);
replacing maxDataPoints with a random name (I just used int a, actually) took care of it. Thanks.
A2J, I tried removing the maxColumns, but the compiler had a fit, and (according to my instructor, anyhow) the second value in a 2d array (the number of columns) has to be defined. So... now I just have to finish writing it and figure out why it thinks aryStudentData is an undeclared identifier...
If you're getting more compiler errors then post more source for us to look at and the exact messages. Is your instructor a "I don't wany any warnings" type of guy? Mine was. I am now, too. Warnings may not be errors, but they might lead to them in the future if not immediately. Best to fix all warnings.
/******************************************************************************** ** CIS 15 BG-03 ** Winter, 2004 *************** ** ** Lab 3 Two-Dimensional Arrays 20 Points ** **************************************************** Reading Assignment: Page 402, Programming Example - Calculate Averages Write a program to keep records and perform statistical analysis for a class of students. The class may have up to 45 students (validation required: see Line 70, page 368). There are five quizzes during the quarter. Each student is identified by a personal identification number (PIN) of four digits. Input : Read data from a text file. The text file has the following format: 3 1110 78 55 80 100 86 1111 67 78 45 89 35 1112 23 66 78 79 90 3 - represents the number of students in the class; 1110 - represents the Personal Identification Number of a student; it is followed by 5 integers representing the quiz scores. Output: Write the output to another text file (see the example below). The output is to contain: 1. Students' statistics (lowest score, largest score, sum, and average) 2. Quizzes' statistics (lowest score, largest score, sum, and average). 3. Class average. Data: Run your program once, using SCORES.TXT Example: If the data file contains data for two students the output should be: STUDENTS: Statistical Analysis PIN Quiz1 Quiz2 Quiz3 Quiz4 Quiz5 | Low High Sum Ave ==== ===== ===== ===== ===== ===== | ==== ==== ==== ===== 1019 90 80 85 100 60 | 60 100 415 83.0 1178 82 90 90 45 89 | 45 90 396 79.2 ================================================================== QUIZZES: Statistical Analysis PIN Quiz1 Quiz2 Quiz3 Quiz4 Quiz5 ==== ===== ===== ===== ===== ===== 1019 90 80 85 100 60 1178 82 90 90 45 89 _______________________________________ Low 82 80 85 45 60 High 90 90 90 100 89 Sum 172 170 175 145 149 Ave 86.0 85.0 87.5 72.5 74.5 ======================================================= CLASS AVERAGE: 81.10 **************************************** ** ** Written By: ??? ** ** Date: 1/26/02 - ??/??/02 ** ********************************************************************************/I'm not done with it at the moment, tho. I figured I'd finish it first, because the other errors could just be because the program isn't done, so there are some loose ends if you will. To answer your question, yes, my instructor is picky about that kind of stuff. You're right- warnings are going to cause problems later. Right now tho, there are no warnings. Just errors.
I have Jury Duty in the morning (how fun) so I doubt I'll be able to see any questions you might have before tomorrow afternoon, but I'm sure others can help if I'm not available.
The compiler doesn't have any problems with #define X 1 and int X = 1;
The reason is the preprocessor actually replaces "X" in the code with "1" so the compiler will never see "X" it will see "1" and it's that which confuses the compiler.
The compiler never saw "maxDataPoints" it only saw "271" and it wasn't expecting that.
Aaaaaaaaanywho, I figured it was easier to start over after I realized that there's a very similar program in the book. I simply scanned it and edited it a bit.
I've got it almost finished. It's giving me two errors tho:
I'll attach the source code and the build log, but I've never understood these "too many arguments for call through..." errors, and this particular code is straight out of the text, so I don't understand why it's blowing up.
Regardless, there's the error, the code and the buildlog are zipped.
I'll look @ the code and see if I can find what the deal is.
Only error I get is when I run the program:
C:\Documents and Settings\Administrator\Desktop\Lab3>bg_lab_3.exe
LAB 3 - Two-Dimensional Arrays
Error opening file
I use Dev-C++. 4.9.8.0 and then I applied the 4.9.8.7 upgrade to the install directory. You can find it at http://www.bloodshed.net
I put the scores.txt file, the updated source and the exe in another zip file (attached)... I also have to set it up so that the program recognizes the first entry in the text file as the number of students in the class. I think I have an idea of how to do it, but I'm not sure. I was going to put a fscanf line in front of the for loops in the getdata function, and set it = to a variable that the for loop uses to determine the number of rows in the array.
Also, getData() has nested for()s which is fine, but your scanf() is outside the inner for() meaning the dataIn is never changed until the the for()s are completely done unrolling they start all over because of the while(), so only until the while() is reached again (after 46 * 6 iterations) is the dataIn variable assigned a new value.
Also, you're not reading the first line as the # of students and you're not taking the first column as the student ID. I'm working on it, though, but I'm doing other studd too so it might be faster if you just fix it yourself. If you don't post that you've fixed it by the time I'm done then I'll post what I've changed and hopefully it will work for you.
2) I replaced almost all instances of MAX_COLS with (MAX_COLS - 1)
Seems to be working.
I'm working on the other two right now.
But if you want to do it w/ pointers:
int* actualRows;
Then, you guessed it, pass "actualRows" =)
Your arrays are really pointers. C/C++ doesn't have arrays, it only has array-notation. It's all pointers. You're already passing pointers, but you're doing it using array notation. Just change notation, or you could (never tried it before) pass an array of 1 int. eg:
int actualRows[1];
Then pass it like you already are. I don't know of any reason that it wouldn't work, though I have never tried a 1-dimensional 1-element array before that I can recall.
Anyhow, here's the program code (I'll also zip and attach it):
/******************************************************************************** ** CIS 15 BG-03 ** Fall, 2003 *************** ** ** Lab 4 Pointer Applications 20 Points ** ************************************************* Write a program that reads the size of an array from the keyboard. The array is to be dynamically allocated in the heap. Then the program reads integers from the keyboard into the array. Modify the straight insertion sort from Chapter 8 so that it sorts the array into ascending order. The program must not change the original array and must not create a copy of it. (See figure 10-25, page 515). Print the sorted array and the original array to a file, using the following format: ========== =========== ============ ORIGINAL ASCENDING DESCENDING ========== =========== ============ 50 20 100 70 50 70 20 50 70 100 70 70 70 70 50 50 70 50 70 100 20 Data : Run your program twice: Test Data Set 1: 10 here you enter the size of the array 10 10 30 30 40 50 50 50 60 60 Test Data Set 2: 25 50 30 10 90 5 20 25 90 40 35 50 30 50 30 90 5 40 50 90 30 10 5 20 10 35 NOTE: Use pointer notation throughout. Do not use an index to access the elements in the array: use a pointer, as in the following example: ____________________________________________________________________ YES int ary[10] = {10, 15, 10, 30, 40, 50, 60, 70, 80, 90 }; int *pWalk; int *pLast; pLast = ary + 10 - 1; for( pWalk = ary; pWalk <= pLast; pWalk++ ) printf( "%3d", *pWalk ); _____________________________________________________________________ NO int ary[10] = {10, 15, 10, 30, 40, 50, 60, 70, 80, 90 }; int i; for( i = 0; i < 10; i++ ) printf( "%3d", *(ary + i) ); **************************************** ** ** Written By: First_Name Last_Name ??? ** ** Date: 2/6/02 - ??? ** ********************************************************************************/ #include < stdio.h > #include < stdlib.h > #include < crtdbg.h > #define maxSize 100 /* Prototype Declarations */ void welcome (void); void farewell (void); void getData (int array [], int *arraySize); void sortArray (int array []); void printArray (int array [], int arraySize); int main (void) { /* Local Definitions */ int array [maxSize]; int arraySize; /* Statements */ welcome (); getData (array [maxSize], &arraySize); printArray (array [maxSize], arraySize); printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Leak\n"); farewell ( ); return 0; } /* main */ /* ============================== welcome ============================== Prints a welcome message. PRE : nothing POST : welcome message printed */ void welcome (void) { /* Local Definitions */ /* Statements */ printf("\t\t LAB 4 - Pointer Applications\n\n"); return; } /* welcome */ /* ============================== farewell ============================== Prints a farewell message. PRE : nothing. POST : farewell message printed */ void farewell (void) { printf("\n\t\tEnd of the program!" "\n\t\tHave a great day!\n"); return; } /* farewell */ void getData (int array [], int *arraySize) { int i; printf("Array Size: "); scanf("%d", &arraySize); for (i = 0; i <= arraySize; i++); { printf("\nValue: "); scanf("%d", &array[i]); } return; } void printArray (int array [], int arraySize) { FILE *fparray; int i; fparray = fopen("array.txt", "w"); fprintf ("fparray", "========== =========== ============\n"); fprintf ("fparray", " ORIGINAL ASCENDING DESCENDING\n"); fprintf ("fparray", "========== =========== ============\n"); for (i = 0; i <= arraySize; i++) { if (i %3 != 0) { fprintf("fparray", "%6d", array[i]); } else { fprintf("fparray", "\n"); fprintf("fparray", "%6d", array[i]); } } scanf("%d", i); } void sortArray (int array []) { return; }It compiles fine, (albiet with warnings, which you can see below), but when it gets to scanning numbers into the array, it crashes.
Here are the warnings:
Better fix that. None that wimpy array[x] crap here!
I'll try to remember to look @ the code before I go to sleep, but I'll probably be going to sleep in about an hour or so so don't hold your breath thinking I'll get it done tonight.
Read my comments because you made numerous errors. Nothing that isn't uncommon, especially for beginning C students but still pitfalls you need to watch for.
/******************************************************************************** ** CIS 15 BG-03 ** Fall, 2003 *************** ** ** Lab 4 Pointer Applications 20 Points ** ************************************************* Write a program that reads the size of an array from the keyboard. The array is to be dynamically allocated in the heap. Then the program reads integers from the keyboard into the array. Modify the straight insertion sort from Chapter 8 so that it sorts the array into ascending order. The program must not change the original array and must not create a copy of it. (See figure 10-25, page 515). Print the sorted array and the original array to a file, using the following format: ========== =========== ============ ORIGINAL ASCENDING DESCENDING ========== =========== ============ 50 20 100 70 50 70 20 50 70 100 70 70 70 70 50 50 70 50 70 100 20 Data : Run your program twice: Test Data Set 1: 10 here you enter the size of the array 10 10 30 30 40 50 50 50 60 60 Test Data Set 2: 25 50 30 10 90 5 20 25 90 40 35 50 30 50 30 90 5 40 50 90 30 10 5 20 10 35 NOTE: Use pointer notation throughout. Do not use an index to access the elements in the array: use a pointer, as in the following example: ____________________________________________________________________ YES int ary[10] = {10, 15, 10, 30, 40, 50, 60, 70, 80, 90 }; int *pWalk; int *pLast; pLast = ary + 10 - 1; for( pWalk = ary; pWalk <= pLast; pWalk++ ) printf( "%3d", *pWalk ); _____________________________________________________________________ NO int ary[10] = {10, 15, 10, 30, 40, 50, 60, 70, 80, 90 }; int i; for( i = 0; i < 10; i++ ) printf( "%3d", *(ary + i) ); **************************************** ** ** Written By: First_Name Last_Name ??? ** ** Date: 2/6/02 - ??? ** ********************************************************************************/ #include < stdio.h > #include < stdlib.h > //#include < crtdbg.h > #define maxSize 100 /* Prototype Declarations */ void welcome (void); void farewell (void); void getData (int array[], int *arraySize); void sortArray (int array[]); void printArray (int array[], int arraySize); int main (void) { /* Local Definitions */ int array [maxSize]; int arraySize; /* Statements */ welcome(); getData(array, &arraySize); printArray(array, arraySize); // printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Leak\n"); farewell(); return 0; } /* main */ /* ============================== welcome ============================== Prints a welcome message. PRE : nothing POST : welcome message printed */ void welcome (void) { /* Local Definitions */ /* Statements */ printf("\t\t LAB 4 - Pointer Applications\n\n"); return; } /* welcome */ /* ============================== farewell ============================== Prints a farewell message. PRE : nothing. POST : farewell message printed */ void farewell (void) { printf("\n\t\tEnd of the program!" "\n\t\tHave a great day!\n"); return; } /* farewell */ void getData (int array[], int *arraySize) { int i; printf("Array Size: "); // &arraySize? HUH? arraySize is a pointer. // You want the address of the pointer? I doubt it. // scanf("%d", &arraySize); scanf("%d", arraySize); // What? arraySize is a pointer! You're comparing the memory location of // arraySize to the value of i. This isn't what you want! // Also, your for() had an ; before the {} meaning it was an EMPTY for // and that you were just scoping the two statements inside the {} but // they weren't scoped with the for(). Be careful of such things. // They're syntactically correct, but not what you intended. // Also, be careful. You're using <= when you need <. // for (i = 0; i <= arraySize; i++); for (i = 0; i < *arraySize; ++i) { printf("\nValue: ", i, *arraySize); scanf("%d", (array + i)); } return; } void printArray (int array[], int arraySize) { FILE *fparray; int i; fparray = fopen("array.txt", "w"); // You had "fparray" (just like that, quoted) which was causing // fprintf() to die. fprintf (fparray, "========== =========== ============\n"); fprintf (fparray, " ORIGINAL ASCENDING DESCENDING\n"); fprintf (fparray, "========== =========== ============\n"); for (i = 0; i <= arraySize; i++) { if (i %3 != 0) { fprintf(fparray, "%6d", *(array + i)); } else { fprintf(fparray, "\n"); fprintf(fparray, "%6d", *(array + i)); } } scanf("%d", i); return; } void sortArray (int array[]) { return; }You'll notice I got rid of all of the array stuff and replaced it with (array + i).