|
|
|||||||
| Computers & Information Technologies « Everything related to computers and internet. » |
![]() |
|
|
Share | Thread Tools | Search this Thread |
|
|
#1 |
|
Registered Member
Last Online: 09-03-2009
Join Date: Mar 2006
Posts: 170
Thanks: 1
Thanked 36 Times in 20 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
|
HI I WROTE A PROGRAM AND I WANT A HELP WITH IT PLEASE IF ANY KNOWS PROGRAMING OR HOW TO WRITE PROGRAM'S HELP ME WITH THIS,WHAT ARE THE ERRORS?: (PROGRAM ABOUT 3 FONCTIONS MOYENNE+MINIMUM+MAXIMUM OF A CLASS AND I WILL ENTER THE NOTES)
#include<iostream.h> float m,i; float z,s,y,x; double maximum(float n[30]); { for(i=1;i<=n;i++) max=n[i]; } double moyenne (float a,int n) { for(i=1;i<=n;i++) s=s+n[i]; m=s/n; } double minimum (float n[30]); { min=n[0]; for(i=1;i<=n;i++) if(n[i]<n[i-1]) min=n[i]; } int main() { float n; do{ cout<<"Entrez le nombre des eleves (maximum=30 "<<endl;cin>>n; }while(n>30); cout<<"Entrez les notes:"<<endl; for(i=1;i<=n;i++) cin>>n[i]; cout<<"La note maximal est:"<<endl; y=maximum(n[i]); cout<<y<<endl; cout<<"La note minimal est:"<<endl; z=minimum(n[i]); cout<<z<<endl; cout<<"La moyenne des notes est:"<<endl; x=moyenne(a,n); cout<<x<<endl; return 0; }
__________________
Lebanese Army...Atyab Gech. May GOD protect Our Officers and soldiers... |
|
|
|
|
|
#2 |
|
Super Moderator
Last Online: 02-16-2022
Join Date: May 2006
Posts: 5,580
Thanks: 1,888
Thanked 2,653 Times in 1,593 Posts
Groans: 55
Groaned at 35 Times in 32 Posts
|
i'll help you, but i need a little bit of concentration==> at home and not here at the uni.
expect an answer at night.
__________________
click on 'Groan' to switch to my left testicle. |
|
|
|
|
|
#3 |
|
Registered Member
Last Online: 05-30-2010
Join Date: Dec 2006
Posts: 131
Thanks: 1
Thanked 3 Times in 2 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
|
Well... the code is wrong.. there's lots of mistakes...
but before i correct it, i need to know wat do u want the program to do? to you have the number of students? if so wat is it? so ur program needs to asks for the number of students. then enter the marks for each student.. get max, min, and average...??? am i right?
__________________
Cheers, Simon |
|
|
|
|
|
#4 | ||||
|
Registered Member
Last Online: 05-30-2010
Join Date: Dec 2006
Posts: 131
Thanks: 1
Thanked 3 Times in 2 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
|
where can i start???
Quote:
and inorder to get max, you should compare the contents of the array. somethin like Code:
double temp;
temp = n[0];
for (int i = 1; n < arraysize; i++)
{
if (n[i] > temp)
temp = n[i]
}
return temp;
Quote:
i'll explain what the function is expecting. Code:
double moyenne (float a[], int n) instead of using n[i], u should use a[i] Quote:
i'm not sure if i should give you the code i wrote, or get u to read the compiler errors, and research them so u can learn about your mistakes. Quote:
then i can just give u my code. take care
__________________
Cheers, Simon |
||||
|
|
|
|
|
#5 |
|
Registered Member
Last Online: 05-30-2010
Join Date: Dec 2006
Posts: 131
Thanks: 1
Thanked 3 Times in 2 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
|
Code:
#include <iostream>
using namespace std;
double getMax(double arr[], int arrSize)
{
double tempmax = arr[0]; // assigning max to first element of the array
for (int i = 1; i < arrSize ; i++)
{
if ( tempmax < arr[i] )
tempmax = arr[i];
}
return tempmax;
}
double getMin(double arr[], int arrSize)
{
double tempmin = arr[0]; // assigning max to first element of the array
for (int i = 1; i < arrSize ; i++)
{
if ( tempmin > arr[i] ) // check if next element in array is less than min
tempmin = arr[i];
}
return tempmin;
}
double getAvrg(double arr[], int arrSize)
{
double avg;
double total = 0;
for (int i = 0; i < arrSize ; i++)
total = total + arr[i];
avg = total / arrSize;
return avg;
}
int main()
{
int studentNum;
double min, max, avg;
cout << "\nPlease enter number of students: ";
cin >> studentNum;
// check if num of students is less than 30
while (studentNum > 30 || studentNum < 0 )
{
cout << "\nPlease enter a positive number less than 30: ";
cin >> studentNum;
}
double* studentArray = new double[studentNum];
cout << "Please enter the score for each student" << endl;
for (int i = 0; i < studentNum; i++)
{
cout << "Enter score for student " << i+1 << ":" ;
cin >> studentArray[i];
}
max = getMax( studentArray, studentNum);
min = getMin( studentArray, studentNum);
avg = getAvrg( studentArray, studentNum);
cout << "\n\nThe max score is: " << max << endl;
cout << "\n\nThe min score is: " << min << endl;
cout << "\n\nThe avg score is: " << avg << endl;
return 0;
}
there is no limit for the score entered by the student... u might want to do more checks... pls understand the code or u won't get better at it..
__________________
Cheers, Simon |
|
|
|
|
|
#6 |
|
Super Moderator
Last Online: 02-16-2022
Join Date: May 2006
Posts: 5,580
Thanks: 1,888
Thanked 2,653 Times in 1,593 Posts
Groans: 55
Groaned at 35 Times in 32 Posts
|
i tried to correct your program but i got lost like hell, there are lots of mistakes in your
code, and i kept getting compiling errors even after i corrected a dozen of mistakes. but i'll criticise your work, remember that i promised to help you. first, the 3 functions you declared are not VOID, so they need a return statement, you missed all three returns. the logic in function minimum is good, or it is the best way to do it , but function maximum is a whole mess. use the same logic as minimum.the function moyenne needs to: set s to zero at first, or it will start as a random number. now to main; the do while statement is a mess, no need for do while at all, only write a while statement that asks for a number between 1 and 30. you need a while statement to assign each grade for the students. x, y and z are being assigned to nothing ( remember the missing return statements ). anyway there is no need to them, only use: cout<<maximum (n[i],studentnum) now let me make some remarks: to process an array with a for loop, start from i=0 and not 1, to i<arraysize and not <= arraysize. an array is passed by reference to a function when coding a function that will process an array, always think that this function will take 2 parameters at least: the array and its size. now all i have to say is that sayo did an extremely proffesional job ( remember that he is the computer engineer, and i'm still a 1st yeat comp.eng student ) and your teacher wont believe that this is your own work, so try to correct your code as much as you can, and use sayo's code for help. now a question to sayo; double* studentArray = new double[studentNum]; <<<< what is that for????
__________________
click on 'Groan' to switch to my left testicle. |
|
|
|
|
|
#7 |
|
Registered Member
Last Online: 09-03-2009
Join Date: Mar 2006
Posts: 170
Thanks: 1
Thanked 36 Times in 20 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
|
10x man 2ded ma3koun kelkoun 100/100?
![]() ![]() lol
__________________
Lebanese Army...Atyab Gech. May GOD protect Our Officers and soldiers... |
|
|
|
|
|
#8 |
|
Super Moderator
Last Online: 02-16-2022
Join Date: May 2006
Posts: 5,580
Thanks: 1,888
Thanked 2,653 Times in 1,593 Posts
Groans: 55
Groaned at 35 Times in 32 Posts
|
#include<iostream>
using namespace std; double m,s; double maximum(double n[],int num) { double max; max = n[0]; for(int i=1;i<num;i++) { if ( n[i] > max ) max=n[i]; } return max; } double moyenne (double n[] , int num) { double s; for(int i=0;i<num;i++) s=s+n[i]; m=s/num; return m; } double minimum (double n[], int num) { double min=n[0]; for(int i=1;i<num;i++) if(n[i]<min) min=n[i]; return min; } int main() { int x; cout<<"Entrez le nombre des eleves (maximum=30 "<<endl;cin>>x; while(x>30 || x<1 ) cout<<"entrer un nombre qui est entre 1 et 30"; double n[x]; cout<<"Entrez les notes:"<<endl; for(int i=0;i<x;i++) { cout<<"pour l'eleve numero "<<i+1<<" :"; cin>>n[i]; } cout<<"La note maximal est:"<<maximum(n, x)<<endl; cout<<"La note minimal est:"<<minimum(n, x)<<endl; cout<<"La moyenne des notes est:"<<moyenne(n, x)<<endl; cin.get(); cin.ignore(); return 0; } and finally i managed to correct it ![]() ![]() ![]() ![]() ![]() ![]()
__________________
click on 'Groan' to switch to my left testicle. |
|
|
|
|
|
#9 | |||
|
Registered Member
Last Online: 05-30-2010
Join Date: Dec 2006
Posts: 131
Thanks: 1
Thanked 3 Times in 2 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
|
Quote:
Chriseid06, i'm a Computer Engineer, but my code is really simplified... and i believe it should be suitable for a first year student, i wouldn't expect anything less from my students when i used to tutor. always make ur declarations understandable for any reader, so instead of declaring an array like this: double n[10]; you should use something like this: int array_size = 10; double studArray[array_size]; it tells the reader wat the array is created for. Quote:
Sometimes under windows (visual studio .net is wat i used) you can't create an array with variable length. so you have to create a pointer first then point it to a newly created array. i see in ur code you did the following: cout<<"entrer un nombre qui est entre 1 et 30"; double n[x]; if this compiled with you, then its great.. cout<<"entrer un nombre qui est entre 1 et 30"; double* n= new double[x]; Quote:
m and s should be declared in the "moyenne" function... other than this small thing, your code is very nice and neat
__________________
Cheers, Simon Last edited by sayo9394; 01-11-2007 at 01:11 AM. |
|||
|
|
|
|
|
#10 |
|
Registered Member
Last Online: 04-15-2018
Join Date: Feb 2006
Posts: 624
Thanks: 2
Thanked 299 Times in 276 Posts
Groans: 0
Groaned at 1 Time in 1 Post
|
sayo9394 u forgot to delete ur array.
at the end of the code: delete []studentArray; In addition I do not know if the program needs 3 functions, for now u r doing 3 for loops, the min, max and avg can be done in 1 for loop.
__________________
I pwn n00bs |
|
|
|
![]() |
|
| Tags |
| helpc |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|