Join 136,053 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,572 people online right now. Registration is fast and FREE... Join Now!
Write a program to answer inquires about student data. Using a menu driven interface, provide the capability to print out the scores, average, or grade for a student. A fourth menu option is to provide all information about a given student. All array functions are to receive the array as a pointer and use pointer arithmetic. The data will be stored in a 2d array...
Here's my checklist for what I've already done:
Menu Driven Interface: Done!
Opened file stddata.txt and stored data in variables: snum, qz1, qz2, qz3, qz4, qz5: Done!
Stored variables from previous step in a 2d array stddata[15][6]: NOT DONE!
This is where I'm stuck. I'm not sure how to properly initialize a 2d array in a function (At both spots function declaration & function initialization), and then set column 1=snum, column 2=qz1, column 3=qz2, column 4=qz3, column 5=qz4, column 6=qz5.
Just to show a little effort on my part, here's what I've tried writting:
cpp
//Function Declaration void setequal (int* stable, int snum, int qz1, int qz2, int qz3, int qz4, int qz5);
void setequal (int* stable, int snum, int qz1, int qz2 ,int qz3, int qz4, int qz5) { int r = 15, c = 6; int row, column; int stddata[r][c];
*stable was intended to be a pointer to that array so that I could just use it throughout the rest of my program but I'm not sure if I did that right. I also wanted to make sure that this code would do what I think it would and place the values that I scanned in from file into the array.
The next step will be to create a search algorithm, and by chance is there a way to do this and make it into a header file. Searching seems to be a major part of C programs and it would be nice to be able to include that as a header file to simplify programming techniques.
Anyways thanks for any help you can give, and please make sure ideas posted are done in C language. Thanks
This post has been edited by webmin: 1 Aug, 2008 - 12:06 PM
Ok I've sort of answered one of my own questions. I know that to get the info I need into my array I need to use the sprintf command under stdio.h I also found the search.h header file and how to use that properly.
Now I'm down to two questions that I can think of. The first one is can someone show me the proper way to use this sprintf with my code or something simliar to it so that I can get an idea of what I need to implement in my own program.
Question two: when I search for the student number, how can I get it to also give the row that this student corresponds to.
I've decided against printing the values into my array. So now all I need to know is how can I search for a student, and have it return the row to me as well.
I'd also like to be able to take the last 5 values in that row and either print them, get an average & print that average, or get an average, convert it to a letter grade & print the letter grade.
Any suggestions would be helpful as long as they're in C. Thanks
void menu(); int search (void); int compare (int *x, int *y) { return (* x -*y); } int printscr (void); int printavg (void); int printgrd (void); int printinfo (void); int extprog (void);
int main (void) { int select, selectIn;
do { menu(); printf("Please choose option 1-5:\n"); scanf_s("%d", &select);
switch(select) { case 1: selectIn = printscr(); break; case 2: selectIn = printavg(); break; case 3: selectIn = printgrd(); break; case 4: selectIn = printinfo(); break; case 5: selectIn = extprog();/*system("cls"); printf("Have a nice day.\n"); selectIn = 0;*/ break; default: FLUSH; system("cls"); printf("\aNot recognized. Please try again.\n"); selectIn = 1; } } while(selectIn); return 0;
} void menu (void) { printf("\t\t\tWelcome to Grade Book V1.0.\n\t\tPlease tell me what we're going to do today?\n\n"); printf("\t\t*********************************************\n"); printf("\t\t* 1:Print A Students Scores *\n"); printf("\t\t* 2:Print A Students Average *\n"); printf("\t\t* 3:Print A Students Letter Grade *\n"); printf("\t\t* 4:Look up a Students info *\n"); printf("\t\t* 5:Quit *\n"); printf("\t\t*********************************************\n");
Now in the search function I would like to create a pointer such as *snumber and set that equal to sdata[0-14][0]. Meaning that I would like to be able to enter a student number such as 1234, and it would return values 52, 7, 100, 78, 34 with it as well.
I would also like to be able to take those returned values and calculate things like an average, a grade, or just print the scores.
Basically I need to be able to extract a row from a 2d array.
if someone can point me in the right direction I'd be very happy to finish this project. Thanks.
This post has been edited by webmin: 2 Aug, 2008 - 11:24 AM
void menu(); int search (void); int compare (int *x, int *y) { return (* x -*y); } int printscr (void); int printavg (void); int printgrd (void); int printinfo (void); int extprog (void); void displayStudentInfo(int studentnumber, int sdata[][6]);
int main (void) { int select, selectIn;
do { menu(); printf("Please choose option 1-5:\n"); scanf_s("%d", &select);
switch(select) { case 1: selectIn = printscr(); break; case 2: selectIn = printavg(); break; case 3: selectIn = printgrd(); break; case 4: selectIn = printinfo(); break; case 5: selectIn = extprog();/*system("cls"); printf("Have a nice day.\n"); selectIn = 0;*/ break; default: FLUSH; system("cls"); printf("\aNot recognized. Please try again.\n"); selectIn = 1; } } while(selectIn); return 0;
} void menu (void) { printf("\t\t\tWelcome to Grade Book V1.0.\n\t\tPlease tell me what we're going to do today?\n\n"); printf("\t\t*********************************************\n"); printf("\t\t* 1:Print A Students Scores *\n"); printf("\t\t* 2:Print A Students Average *\n"); printf("\t\t* 3:Print A Students Letter Grade *\n"); printf("\t\t* 4:Look up a Students info *\n"); printf("\t\t* 5:Quit *\n"); printf("\t\t*********************************************\n");
void displayStudentInfo(int studentnumber, int sdata[][6]) { int studentID = studentnumber; /* * The number is found, iterate through array * to display associated values */ for (int i = 0; i < 15; i++) { if (sdata[i][0] == studentID) { printf("Associated data with student: %d", studentnumber); printf("\n"); for (int j = 1; j < 6; j++) { printf("%d", sdata[i][j]); printf("\n"); } } } }
Modified parts include:
cpp
void displayStudentInfo(int studentnumber, int sdata[][6]) { int studentID = studentnumber; /* * The number is found, iterate through array * to display associated values */ for (int i = 0; i < 15; i++) { if (sdata[i][0] == studentID) { printf("Associated data with student: %d", studentnumber); printf("\n"); for (int j = 1; j < 6; j++) { printf("%d", sdata[i][j]); printf("\n"); } } } }
It compares the search value to the hard coded array you already had and displays associated information. You can format it anyway you want--I'll leave it up to you
Unless its a project assignment, there is no need for a pointer to the array, you can do averages, etc... once you have located the correct 2nd dimension of the array through search.
If I read correctly, this is what you wanted. Enjoy!
My only concern is that my book states that all array functions are to receive the array as a pointer and use pointer arithmetic.
Does your solution do this or because I'm not using any functions that calls an array this step isn't neccessary?
I guess I could always declare *stable in the function declaration, and then in the actual function itself declare *stable = sdata[][6]
One more question, your void function that you built, where should that be called into the program, I didn't see it called by search, or by main, or any other function for that matter. Is it portable enough that I could do something like
cpp
int printscr (void) { search(); displayStudentInfo(int studentnumber, int sdata[][6]); ... }
Again thanks for your help
This post has been edited by webmin: 2 Aug, 2008 - 06:21 PM
sorry here's my edit while you made your new post:
QUOTE
I guess I could always declare *stable in the function declaration, and then in the actual function itself declare *stable = sdata[][6]
One more question, your void function that you built, where should that be called into the program, I didn't see it called by search, or by main, or any other function for that matter. Is it portable enough that I could do something like
cpp view plainprint?
1. int printscr (void) 2. { 3. search(); 4. displayStudentInfo(int studentnumber, int sdata[][6]); 5. ... 6. }
int printscr (void) { search(); displayStudentInfo(int studentnumber, int sdata[][6]); ... }
Search calls the display function if the number is found:
cpp
if (find) { printf ("Student %d found in linear search.\n", studentnumber); displayStudentInfo(studentnumber, sdata[][6]); }
So a successful search will display the associated values if you so chose.Simply take that function call out of search qand pop it whereever you choose
edited for a typo
This post has been edited by KYA: 2 Aug, 2008 - 06:26 PM
I see that's what I did. So now I need to look over this to get it to calculate an average, or a grade. If you have any suggestions, I'd appreciate them.
Declare a few temp variables for avg, etc.. inside display (or outside and pass them in) Inside the second for loop calculate the second deminsion values as ypou iterate through them.
Declare a few temp variables for avg, etc.. inside display (or outside and pass them in) Inside the second for loop calculate the second deminsion values as ypou iterate through them.
Boom--done!
Not quite sure I follow
I should declare
cpp
float avg; int sum;
for (int i = 0; i < 15; i++) sum += sdata[][6]; // not sure if this is right avg = (float)sum / 5;