473,796 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

whats wrong with my function?

43 New Member
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 2368
joestevens232
43 New Member
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 Recognized Expert Moderator Expert
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
joestevens232
43 New Member
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
joestevens232
43 New Member
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
joestevens232
43 New Member
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 Recognized Expert Moderator Expert
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
joestevens232
43 New Member
got it...thank you!!!
May 3 '07 #8
joestevens232
43 New Member
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 Recognized Expert Moderator Expert
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

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

Similar topics

6
3472
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) Case "Authenticated" Case "CI_CODE" Case "organisation_description" Case "location_description"
2
11437
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 suggestion? If I call the function like this: wait(500); it should wait 500ms right? function wait(time) { while(1){ setTimeout("break;",time); } }
1
2751
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 bit-by-bit, XORing the most significant bit of the FCS with the msb of the character. All comments are welcome. Yes, this is for a school assignment. If I wanted to just grab some googled code I would; I want to actually understand what I am doing...
6
3117
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
2836
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 value by casting that functions with appropriate data type?
7
2233
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 me why? Its probably sooooooo obvious, but it is 1.19 am! Thanks. www.thunderin.co.uk/
2
1932
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). (the included config is just server settings, the login is just a login form). The script appear to connect but will not redirect users, it seems that even with correct login details, it won't validate.
2
1136
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
1725
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 statement in the for(;;) loop below, then no issue. Any help would be appreciated! function getStuff()
0
9525
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10221
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7546
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.