| | |
arrays and functions
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2006
Posts: 7
Reputation:
Solved Threads: 0
I am having a huge problem with writing this program. Every time I ask the teacher for help she is too busy helping someone else. so she just tell me one thing and runs off to the next person without checking if it worked and what other help I might need. It has become very frustrating. I have been able to ask for a classmates help, but that is only when we have free time during class, if he's not too busy writing his own code. I am turning to you for help. I have written and adjusted this program so many times that I am so confused. Please take a look at my code and steer me in the right direction. I greatly appreciate it. Thank you. I also hope that I use these code tags the right way. Sorry if it doesn't work. Oh, and P.S. the teacher said we had to call a function and pass something by reference(what, I'm not sure, but I specifically remember her pointing it out) I'm thinking it's to do the calculations. I've tried to design this program using only function, but I'm not that great with those, so it's been difficult.
C++ Syntax (Toggle Plain Text)
//chapter 7 lab p475 ex8 gradebook //Intructions: //A teacher has five students who have taken four tests. //The teacher uses the following grading scale to assign a letter //grade to a student, based on the average of his or her test scores. //___________________________________ // Test Score Letter Grade //----------------------------------- // 90-100 A // 80-89 B // 70-79 C // 60-69 D // 0-59 F //___________________________________ //Write a program that uses a two-dimensional array of charachters //that hold the five student names, a single-dimensional array of //five characters to hold the five students' letter grades, and five //single-dimensional arrays of four doubles to hold each student's //set of test scores. //The program should allow the user to enter each student's name and //his or her scores. It should then calculate and display each //student's average test score and a letter grade based on that average. //Imput Validation: Do not accept test scores less than zero or greater //than 100. #include <iostream> using namespace std; //function prototype void calcdata(double []); void display data(); //start of main int main() { const int NUM_NAMES = 5; //how many occurances const int NAMESIZE = 11; //how long names can be, 10 letters const int NUM_TESTS = 4; //how many tests char name[NUM_NAMES][NAMESIZE]; //two-dimensional name array char grade[NUM_NAMES]; //grade for each student array double average[NUM_NAMES]; //average for each student array double student1[NUM_TESTS] //student1 array double student2[NUM_TESTS] //student2 array double student3[NUM_TESTS] //student3 array double student4[NUM_TESTS] //student4 array double student5[NUM_TESTS] //student5 array cout << βEnter the studentβs nameβ? for (int count = 0; count < NUM_NAMES; count++) { // beginning of nested for loop cout << "Student" << (count +1) <<": "; cin >> name[count]; for (i = 0; i < NUM_NAMES; i++) cout >> name [i]; //stub statement } //end of for loop //Beginning of validation sequence taken from chapter 6 lab assignment //********Need to change to fit with this lab assignment******** cout << "Please enter test 1, with scores between 0 and 100.\n"; cin >> val1; while (val1 < 0 || val1 > 100) { cout << "You have not entered a number between 0 and 100. Please re-enter test 1.\n"; cin >> val1; }//end of while loop cout << "Please enter test 2, with scores between 0 and 100.\n"; cin >> val1; while (val2 < 0 || val2 > 100) { cout << "You have not entered a number between 0 and 100. Please re-enter test 2.\n"; cin >> val2; }//end of while loop cout << "Please enter test 3, with scores between 0 and 100.\n"; cin >> val1; while (val3 < 0 || val3 > 100) { cout << "You have not entered a number between 0 and 100. Please re-enter test 3.\n"; cin >> val3; }//end of while loop cout << "Please enter test 4, with scores between 0 and 100.\n"; cin >> val4; while (val4 < 0 || val4> 100) { cout << "You have not entered a number between 0 and 100. Please re-enter test 4.\n"; cin >> val4; }//end of while loop cout << "Please enter test 5, with scores between 0 and 100.\n"; cin >> val5; while (val5 < 0 || val5 > 100) { cout << "You have not entered a number between 0 and 100. Please re-enter test 5.\n"; cin >> val5; }//end of while loop //display output for validation cout << "Test 1 is: " << val1 << endl; cout << "Test 2 is: " << val2 << endl; cout << "Test 3 is: " << val3 << endl; cout << "Test 4 is: " << val4 << endl; cout << "Test 5 is: " << val5 << endl; //call to function calcdata calcdata(); return 0; } //end of main //************************************************ //function definition of calcdata // this function is going to get the test scores // array and calculate the averages // and calculate grade //i need to pass the student names to correspond // with the test scores, avgs, and grade //************************************************ void calcdata() const int NUM_STUDENTS = 5; const int NUM_SCORES = 4; double total, average; double scores[NUM_STUDENTS][NUM_SCORES]; //get each students average score for (int row = 0; row < NUM_STUDENTS; row++) { //set the accumulator. total = 0; //sum a row for (int col = 0; col < NUM_SCORES; col++ total += scores[row][column]; //get the average average = total / NUM_SCORES; // //************************************************** // loop used to determine grade for each students average //for (int index = 0; index < NUM_NAMES; index++) //nested loop //for (int count =0; index < NUM_NAMES; count++) if (average[0] < 60) grade[0] = βFβ else if (average [0] < 70) grade[0] = βDβ else if (average [0] < 80) grade[0] = βCβ else if (average [0] < 90) grade[0] = βBβ else (average [0] <= 100) grade[0] = βAβ if (average[1] < 60) grade[1] = βFβ else if (average [1] < 70) grade[1] = βDβ else if (average [1] < 80) grade[1] = βCβ else if (average [1] < 90) grade[1] = βBβ else (average [1] <= 100) grade[1] = βAβ if (average[2] < 60) grade[2] = βFβ else if (average [2] < 70) grade[2] = βDβ else if (average [2] < 80) grade[2] = βCβ else if (average [2] < 90) grade[2] = βBβ else (average [2] <= 100) grade[2] = βAβ if (average[0] < 60) grade[3] = βFβ else if (average [3] < 70) grade[3] = βDβ else if (average [3] < 80) grade[3] = βCβ else if (average [3] < 90) grade[3] = βBβ else (average [3] <= 100) grade[3] = βAβ if (average[0] < 60) grade[4] = βFβ else if (average [4] < 70) grade[4] = βDβ else if (average [4] < 80) grade[4] = βCβ else if (average [4] < 90) grade[4] = βBβ else (average [4] <= 100) grade[4] = βAβ //call to function displaydata displaydata(); return 0; }//end of function calc data //*************************************************** //definition of function displaydata //this function is designed to display the student's // average and letter grade //*************************************************** void displaydata() for (int i = 0; i < NUM_SIZE; i++) cout << "Student: " <<name [i] <<"average: " << average [i] <<"grade: " << grade [i] << endl; return 0; } // end of main
•
•
Join Date: Mar 2006
Posts: 7
Reputation:
Solved Threads: 0
P.S. I think it might be important to mention that I'm required to used Visual Studio.NET to do my programs. I couldn't get it to work on my home computer, so I have only been able to try to run my programs while I'm at school, which isn't very often. We also only run them without debugging. It builds, links, compiles and runs. I am on chapter 7 of my Starting Out with C++ 4th Edition Update. I've basically done looping, functions and arrays. We are not suposed to use any information from later chapters. Searching and sorting arrays is my next chapter, so that might give you an idea of where I'm at in my book. I hope this helps. Thank you.
•
•
Join Date: Feb 2006
Posts: 54
Reputation:
Solved Threads: 2
•
•
•
•
Originally Posted by ahluka
You would do well to indent it better to make it more readable.
It is READABLE & COMPREHENSIBLE for the NORMAL!!
EDIT::
HEY AHLUKA
I AM SORRY ...REALLY SORRY :o FOR WHAT I TYPED...IN A HASTE I MISUNDERSTOOD IT FOR MY POST ..M REALLY SORRY!! MY MISTAKE..AND YOU MADONNAMAMA YOU SHOULD SURELY INDENT THE CODE!!!
Last edited by HackWizz; Apr 4th, 2006 at 12:31 pm. Reason: READ THE NEXT POST
return 0;
Oh of course it is.
The "beginning of for loop" and "end of for loop" on the same line as the brackets is really good commenting too. I mean the loop is so short, how would I ever have noticed where it begins and ends.
Badly formatted code is one sure-fire way to shoot yourself in the foot, as is over-commenting.
The "beginning of for loop" and "end of for loop" on the same line as the brackets is really good commenting too. I mean the loop is so short, how would I ever have noticed where it begins and ends.
Badly formatted code is one sure-fire way to shoot yourself in the foot, as is over-commenting.
In Mother Russia, car drive you.
•
•
Join Date: Mar 2006
Posts: 7
Reputation:
Solved Threads: 0
I'm sorry about the indenting problem. I originally had the program written in word with indents and when I copied it to notepad so that I could open it from my email at school all the indents disappeared.
I also added the beginning of and end of loop comments, because my last program had so many brackets, I started getting confused as to what loops they belonged to. So I started adding the comments to prevent my confusion, not to annoy other readers.
I don't know how to change the if, else statement to a for loop.
When I have school tomorrow, I will check to see what errors I get. I haven't tried to run the program yet because I don't think I'm writing the code right to begin with.
I also added the beginning of and end of loop comments, because my last program had so many brackets, I started getting confused as to what loops they belonged to. So I started adding the comments to prevent my confusion, not to annoy other readers.
I don't know how to change the if, else statement to a for loop.
When I have school tomorrow, I will check to see what errors I get. I haven't tried to run the program yet because I don't think I'm writing the code right to begin with.
•
•
•
•
Originally Posted by madonnamama
I'm sorry about the indenting problem. I originally had the program written in word with indents and when I copied it to notepad so that I could open it from my email at school all the indents disappeared.
Dont use notepad to write code unless you are writing a hello world program. If you are using Visual Studio there is a very good code editor in it that supports syntax highlighting, auto indenting... If you dont have Visual Studio, Dev-Cpp is a good free compiler with a code editor. Personally I use a seperate editor to write code as it loads quickly compared to onethat comes in a Development Environment. I use the IDE only for compiling, and even that I do in the command line when possible, but that depends on personal taste. Some good free editors that with syntax highlighting and auto indenting are Notepad++, TextPad. These are good even when you are programming in another language as most of the document classes are supported in them.
•
•
•
•
Originally Posted by madonnamama
I also added the beginning of and end of loop comments, because my last program had so many brackets, I started getting confused as to what loops they belonged to. So I started adding the comments to prevent my confusion, not to annoy other readers.
•
•
•
•
Originally Posted by madonnamama
I don't know how to change the if, else statement to a for loop.
![]() |
Similar Threads
- Hey Just started new topic with arrays, and having some trouble please help (C++)
- Using functions and arrays to create a program for grades (C++)
- three dimensional arrays (PHP)
- tic tac toe help. (C++)
- Passing arrays of objects to functions (C++)
- bank account (C++)
Other Threads in the C++ Forum
- Previous Thread: logical error
- Next Thread: Going Insane
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






