473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

whats wrong with my function?

Ok everything is good...I read in all my grades into each of the different arrays that they need to go in. Now Im trying to write a function that takes the grades and computes the average on a hundred point scale...for example:
Student 1
7 quiz scores 9 9.33 8 10 5.5 8 10 ==> the 5.5 score should be dropped, returned value: 90.55
6 project scores 20 47.5 47 45 47.5 48 ==> returned value: 94.44
2 exam scores 83 87 ==> returned value: 85.00
14 lab scores 100 98 96 100 98 92 88 96 92 86 92 94 100 96 ==> returned value 94.86

heres what I got:
Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. //Structure for holding all the grades                                          
  9. struct MyGrades
  10. {
  11.     string student;
  12.     float quizzes[7];
  13.     float projects[6];
  14.     float exams[2];
  15.     float labs[14];
  16. };
  17.  
  18. const int tot_quiz = 7;
  19. const int tot_proj = 6;
  20. const int tot_exam = 2;
  21. const int tot_labs = 14;
  22. const int tot_quiz_pts = 70;
  23. const int tot_proj_pts = 300;
  24. const int tot_exam_pts = 200;
  25. const int tot_lab_pts = 150;
  26.  
  27. double ret_avg(float scores, int num_scores, int tot_pts);
  28.  
  29. int main()
  30. {
  31.     // outputting the correct format for all the averages to go                 
  32.     cout << "No. -Name--     --Quiz-- -Project-  -Exam- -Lab- -Total-  Grade"
  33.          << endl;
  34.     cout << "--- ----------- -------- ---------- ------ ----- -------  -----"
  35.          << endl;
  36.     cout << endl;
  37.  
  38.     int student_number = 1; //counter for number of students                    
  39.  
  40.     ifstream in_file;
  41.  
  42.     MyGrades Grades; //declare an instance of a Grades struct                   
  43.     in_file.open("grades.txt");  //opening the grades.txt file                  
  44.  
  45.     if(in_file.fail())  //if function to make sure the file opened              
  46.     {
  47.         cout << "Input file failed to open.\n";
  48.         exit(1);
  49.     }
  50.  
  51.     while (in_file >> Grades.student)  // reads in a string into a variable     
  52.     {
  53.         //reads in all the grades into each seperate array                      
  54.         in_file >> Grades.quizzes[0] >> Grades.quizzes[1] >>
  55.             Grades.quizzes[2] >> Grades.quizzes[3] >> Grades.quizzes[4] >>
  56.             Grades.quizzes[5] >> Grades.quizzes[6] >> Grades.projects[0] >>
  57.             Grades.projects[1] >> Grades.projects[2] >> Grades.projects[3] >>
  58.             Grades.projects[4] >> Grades.projects[5] >> Grades.exams[0] >>
  59.             Grades.exams[1] >> Grades.labs[0] >> Grades.labs[1] >>
  60.             Grades.labs[2] >> Grades.labs[3] >> Grades.labs[4] >>
  61.             Grades.labs[5] >> Grades.labs[6] >> Grades.labs[7] >>
  62.             Grades.labs[8] >> Grades.labs[9] >> Grades.labs[10] >>
  63.             Grades.labs[11] >> Grades.labs[12] >> Grades.labs[13];
  64.  
  65.         cout << " " << student_number << ".  " << Grades.student << "       "<<\
  66.  ret_avg(Grades.quizzes, tot_quiz, tot_quiz_pts) << endl;
  67.  
  68.  
  69.  
  70.         student_number++;
  71.     }
  72.  
  73. }
  74.  
  75. double ret_avg(float* scores, int num_scores, int tot_pts)
  76. {
  77.     double sum = 0;
  78.     double avg = 0;
  79.     for (int i = 0; i < num_scores; i++)
  80.     {
  81.         scores[i] += sum;
  82.     }
  83.     avg = sum / tot_pts;
  84.     return (avg * 100);
  85. }
  86.  
heres the error message i get when i run it:
In function `int main()':
grader.cpp:71: error: cannot convert `float*' to `float' for argument `1' to `double ret_avg(float, int, int)'

and heres the output when run without calling the function in the cout
Expand|Select|Wrap|Line Numbers
  1. No. -Name--     --Quiz-- -Project-  -Exam- -Lab- -Total-  Grade
  2. --- ----------- -------- ---------- ------ ----- -------  -----
  3.  
  4.  1.  Smith       
  5.  2.  Jones       
  6.  
I just want to know what's wrong with this function becuse I thought it would work perfectly but I guess I got something wrong in their. Also if anyone has any suggestions on how Im going to write a function to drop the lowest quiz from the quiz array and then run it in my ret_avg array to get the average of it on a scale of 100. Any help will be great!!!
May 2 '07 #1
10 2331
Okay I just tried something else but its outputting a zero when i call it and print it out...i get a 0 under --Quiz--
here it is:...i donno which is closer...my 1st func or this one.
Expand|Select|Wrap|Line Numbers
  1.     while (in_file >> Grades.student)  // reads in a string into a variable     
  2.     {
  3.         //reads in all the grades into each seperate array                      
  4.         in_file >> Grades.quizzes[0] >> Grades.quizzes[1] >>
  5.             Grades.quizzes[2] >> Grades.quizzes[3] >> Grades.quizzes[4] >>
  6.             Grades.quizzes[5] >> Grades.quizzes[6] >> Grades.projects[0] >>
  7.             Grades.projects[1] >> Grades.projects[2] >> Grades.projects[3] >>
  8.             Grades.projects[4] >> Grades.projects[5] >> Grades.exams[0] >>
  9.             Grades.exams[1] >> Grades.labs[0] >> Grades.labs[1] >>
  10.             Grades.labs[2] >> Grades.labs[3] >> Grades.labs[4] >>
  11.             Grades.labs[5] >> Grades.labs[6] >> Grades.labs[7] >>
  12.             Grades.labs[8] >> Grades.labs[9] >> Grades.labs[10] >>
  13.             Grades.labs[11] >> Grades.labs[12] >> Grades.labs[13];
  14.  
  15.         ret_avg(Grades, tot_quiz, tot_quiz_pts, average);
  16.         cout << " " << student_number << ".  " << Grades.student << "       "<<\
  17.  average <<  endl;
  18.  
  19.  
  20.  
  21.         student_number++;
  22.     }
  23.  
  24. }
  25.  
  26. double ret_avg(MyGrades& data, int num_scores, int tot_pts, double avg)
  27. {
  28.     double sum = 0;
  29.     avg = 0;
  30.     for (int i = 0; i < num_scores; i++)
  31.     {
  32.         data.quizzes[i] += sum;
  33.     }
  34.     avg = sum / tot_pts;
  35.     return (avg * 100);
  36. }
  37.  
May 2 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Your function prototype says:
Expand|Select|Wrap|Line Numbers
  1. double ret_avg(float scores, int num_scores, int tot_pts);
  2.  
but you function says:
Expand|Select|Wrap|Line Numbers
  1. double ret_avg(float* scores, int num_scores, int tot_pts)
  2. etc...
  3.  
Fix your prototype.
May 2 '07 #3
ya disregard that part....i have it fixed in my program...I was just tryin stuff and I must of accidentally posted that version. something else is wrong with it.
May 2 '07 #4
heres what i currently have in my code: the function is just printing out a 0 for the quizzes for both students.
Expand|Select|Wrap|Line Numbers
  1. const int tot_quiz = 7;
  2. const int tot_proj = 6;
  3. const int tot_exam = 2;
  4. const int tot_labs = 14;
  5. const int tot_quiz_pts = 70;
  6. const int tot_proj_pts = 300;
  7. const int tot_exam_pts = 200;
  8. const int tot_lab_pts = 150;
  9.  
  10. double ret_avg(float scores[], int num_scores, int tot_pts, double avg);
  11.  
  12. int main()
  13. {
  14.     // outputting the correct format for all the averages to go                 
  15.     cout << "No. -Name--     --Quiz-- -Project-  -Exam- -Lab- -Total-  Grade"
  16.          << endl;
  17.     cout << "--- ----------- -------- ---------- ------ ----- -------  -----"
  18.          << endl;
  19.     cout << endl;
  20.  
  21.     double average = 0;
  22.     int student_number = 1; //counter for number of students                    
  23.  
  24.     float exams[2];
  25.     float labs[14];
  26. };
  27.  
  28. const int tot_quiz = 7;
  29. const int tot_proj = 6;
  30. const int tot_exam = 2;
  31. const int tot_labs = 14;
  32. const int tot_quiz_pts = 70;
  33. const int tot_proj_pts = 300;
  34. const int tot_exam_pts = 200;
  35. const int tot_lab_pts = 150;
  36.  
  37. double ret_avg(float scores[], int num_scores, int tot_pts, double avg);
  38.  
  39. int main()
  40. {
  41.     // outputting the correct format for all the averages to go                 
  42.     cout << "No. -Name--     --Quiz-- -Project-  -Exam- -Lab- -Total-  Grade"
  43.          << endl;
  44.     cout << "--- ----------- -------- ---------- ------ ----- -------  -----"
  45.          << endl;
  46.     cout << endl;
  47.  
  48.     double average = 0;
  49.     int student_number = 1; //counter for number of students                    
  50.  
  51.  
  52.     ifstream in_file;
  53.  
  54.     MyGrades Grades; //declare an instance of a Grades struct                   
  55.     in_file.open("grades.txt");  //opening the grades.txt file                  
  56.  
  57.     if(in_file.fail())  //if function to make sure the file opened              
  58.     {
  59.         cout << "Input file failed to open.\n";
  60.         exit(1);
  61.     }
  62.  
  63.     while (in_file >> Grades.student)  // reads in a string into a variable     
  64.     {
  65.         //reads in all the grades into each seperate array                      
  66.         in_file >> Grades.quizzes[0] >> Grades.quizzes[1] >>
  67.             Grades.quizzes[2] >> Grades.quizzes[3] >> Grades.quizzes[4] >>
  68.             Grades.quizzes[5] >> Grades.quizzes[6] >> Grades.projects[0] >>
  69.             Grades.projects[1] >> Grades.projects[2] >> Grades.projects[3] >>
  70.             Grades.projects[4] >> Grades.projects[5] >> Grades.exams[0] >>
  71.             Grades.exams[1] >> Grades.labs[0] >> Grades.labs[1] >>
  72.             Grades.labs[2] >> Grades.labs[3] >> Grades.labs[4] >>
  73.             Grades.labs[5] >> Grades.labs[6] >> Grades.labs[7] >>
  74.             Grades.labs[8] >> Grades.labs[9] >> Grades.labs[10] >>
  75.             Grades.labs[11] >> Grades.labs[12] >> Grades.labs[13];
  76.  
  77.         ret_avg(Grades.quizzes, tot_quiz, tot_quiz_pts, average);
  78.         cout << " " << student_number << ".  " << Grades.student << "       "<<\
  79.  average <<  endl;
  80.  
  81.  
  82.  
  83.         student_number++;
  84.     }
  85.  
  86. }
  87.  
  88. double ret_avg(float scores[], int num_scores, int tot_pts, double avg)
  89. {
  90.     double sum = 0;
  91.     avg = 0;
  92.     for (int i = 0; i < num_scores; i++)
  93.     {
  94.         scores[i] += sum;
  95.     }
  96.     avg = sum / tot_pts;
  97.     return (avg * 100);
  98. }
  99.  
May 2 '07 #5
ok i got it to work with :
Expand|Select|Wrap|Line Numbers
  1. double ret_avg(float scores[], int num_scores, int tot_pts)
  2. {
  3.     double sum = 0;
  4.     double avg = 0;
  5.     avg = 0;
  6.     for (int i = 0; i < num_scores; i++)
  7.     {
  8.         sum = scores[i] + sum;
  9.     }
  10.     avg = sum / tot_pts;
  11.     avg = avg * 100;
  12.     return (avg);
  13. }
  14.  
but my output is 85.4714 for the first student and
75 for the second.....how do i get it to be 85.47 and 75.00 i tried using all floats and all doubles in the function. any suggestions?
May 2 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
but my output is 85.4714 for the first student and
75 for the second.....how do i get it to be 85.47 and 75.00 i tried using all floats and all doubles in the function. any suggestions?
Use a manipulator. Check out setprecision, setfill and width member functions of cout.
May 2 '07 #7
got it...thank you!!!
May 3 '07 #8
any ideas on how i can drop one of those quizzes or even just make the lowest score in the array a zero would work for me. any suggestions?
May 3 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
Just loop though the array comparing each element to a test minimum. Start wioth 0 and compare array[0] to array[i]. If array[i] is less than array[0], then i is your new minimum. When you get to the end of the array, you can use the i to set array[i] to any value you want.

For a truly variable array, you need to use a vector.
May 3 '07 #10
Just loop though the array comparing each element to a test minimum. Start wioth 0 and compare array[0] to array[i]. If array[i] is less than array[0], then i is your new minimum. When you get to the end of the array, you can use the i to set array[i] to any value you want.

For a truly variable array, you need to use a vector.

okey dokey

thanks again!!!
May 3 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Colin Steadman | last post by:
I have created a function to kill all session variables that aren't in a safe list. This is the function - Sub PurgeSessionVariables For Each Item In Session.Contents Select Case Trim(Item)...
2
by: Rasmus Grøndahl Olsen | last post by:
I have tried to write a wait function but it seems like it will not brake the while loop. I tried two different solutions. Can anyone tell me what I am doing wrong, and come with another...
1
by: Matthew Wilson | last post by:
I need to write a function crc(msg, len) that gets a char array of length len and then calculates the crc32 for the code. I don't understand what's going wrong in the code I have. It goes...
6
by: thesushant | last post by:
hi, whats the use of third argument to main( ), i.e. environmental parameters.... if i am not wrong ? 1st 1 is argc 2nd is argv and what bout the 3rd 1??????????? sushant
5
by: kernel.lover | last post by:
hello, I want to know if a fuction say malloc is declared as void *malloc() then whats the significance of void here. Does void * is used when function has the flexibility to return any type of...
7
by: Mike Barnard | last post by:
It's a simple test... VERY SIMPLE. But... In an external stlyesheet some attributes don't show. With the same styles cut and pasted to the test internally it works as expected. Anyone tell...
2
by: Nosferatum | last post by:
This script is meant to limit access by sessions, using username and password from mysql db and redirect users after login according to a given value belonging to each user in the db (10,20,30,40)....
2
by: muddasirmunir | last post by:
i want to stop and play server server 2000 using vb6. for this i got one function on internet which is Option Explicit Public Enum ServiceState et_Stop et_Pause et_Start End Enum
5
by: hiqu | last post by:
This issue is driving me nuts and not able to figure out whats wrong. I've this code in my firefox extension. Firefox always hangs and reports the script is busy. if I introduce a break...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.