473,804 Members | 2,008 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
10 2378
joestevens232
43 New Member
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
3475
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
2752
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
3118
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
2840
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
2236
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
1934
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
1138
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
1729
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
9711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10595
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...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9169
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7633
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
5529
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...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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.