Jump to content
Sign in to follow this  
1nub

C++ Help

Recommended Posts

Hey guys I'm doing an assignment for my programming class

 

how would I do a function that uses array's to process calculations?

I need to get the student's total, program average(first three scores), test average(last three scores) and course average(total/6).

So i was thinking i'd set up a loop that would be like

for(int col = 0; col<6 ; col++) for the total

for(int col = 0; col<3 ; col++) for the program average

for(int col = 3; col<6 ; col++) for the test

after that i'm lost...

 

its just six scores like

98 89 75 95 86 97 for the student's grades

 

but the values for my totals and averages have to be like this

 

int total[40] and then two for loops

float averages[40][3] and then two for loops

 

I don't know why I'm using 40 but that's what it said in my assignment.

 

Also since I have to use arrays for my total/averages how would I go about it since my scores are coming from a text file. I'm really stuck right now. :/

Share this post


Link to post
Share on other sites

Ok I'll help you on getting the scores from the file. After that it's just some arithmetic so I am sure you can figure it out. Gimme a few mins.

 

First, you will need to create a stream from your file:

ifstream lStream(lName.c_str());

If you want to also output to the file, you will use fstream instead, which opens the file in both the read and write mode. Also, lName is the file name, with a relative path if I remember correctly.

 

Then you want to read the scores from it. Provided they are written in the same format as your example, you will do a loop, reading one for each iteration and putting them in an array:

int lGrades[6];
for(int i=0; i<=5;i++){
	  lStream >> lGrades[i];
}

 

Hope this helps :)

 

Edit: If there's anything else, let me know. :) Also you're probably not at this level right now, but if you need to do some error detection in that little program, I can help you out on the throw/catch process. Also BTW this would simplify your program if you encapsulated the read from file part into a function.

 

Edit 2: Just to let you know I took that off of the top of my mind. I didn't try to compile anything. Just try to implement this, and if you have some difficulties, just post your entire code and I'll load it into Netbeans.

Edited by The Smith

Share this post


Link to post
Share on other sites

Ok then I guess you understood the principle; before inputting in the int array from the stream, you will input in two strings to get the names.

Share this post


Link to post
Share on other sites

well when you read from the stream, you will first input the names into strings, and then the scores in an int array. So that would be your int scores array...

Share this post


Link to post
Share on other sites

I thought I already had my names in strings when I run the program all the names show up correctly?

Share this post


Link to post
Share on other sites
I thought I already had my names in strings when I run the program all the names show up correctly?

Then where do you take them from? What happens if one guy changes its name in the text file? Will your program adjust accordingly?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...