Vcoderz Community

Vcoderz Community (http://forum.vcoderz.com/index.php)
-   Computers & Information Technologies (http://forum.vcoderz.com/forumdisplay.php?f=6)
-   -   Help[c++] (http://forum.vcoderz.com/showthread.php?t=3549)

OS7 01-10-2007 12:42 PM

Help[c++]
 
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;
}

Kingroudy 01-10-2007 12:55 PM

Re: Help[c++]
 
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.:)

sayo9394 01-10-2007 01:43 PM

Re: Help[c++]
 
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?

sayo9394 01-10-2007 02:51 PM

Re: Help[c++]
 
where can i start???

Quote:

Originally Posted by Chriseid06
double maximum(float n[30]);
{

for(i=1;i<=n;i++)
max=n[i];

}

well, n is an array, its not an int and its not the size of the array... so u can't have i <= n.
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:

Originally Posted by Chriseid06
double moyenne (float a,int n)
{ for(i=1;i<=n;i++)
s=s+n[i];
m=s/n;
}

the above code is good... you should have done the same for the min and max... i think wat u've done is copied and pasted the code...
i'll explain what the function is expecting.
Code:

double moyenne (float a[], int n)
what "a" stands for is the array, and n is the size of the array.
instead of using n[i], u should use a[i]

Quote:

double minimum (float n[30]);
{
min=n[0];
for(i=1;i<=n;i++)
if(n[i]<n[i-1])
min=n[i];
}
good stuff, the logic is correct... again ur using the i <= n, n doesn't hold any value...

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:

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;
}
hmmmm i already finished the code... so i'm gonna let u work a little on fixing your code. then post your new code... I really advice you to read the error statement properly and google it if you don't know what it means.

then i can just give u my code.

take care

sayo9394 01-10-2007 03:54 PM

Re: Help[c++]
 
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;
}

This code works perfectly on Windows. also should work on Linux...
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..

Kingroudy 01-10-2007 06:49 PM

Re: Help[c++]
 
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:D, 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????

OS7 01-10-2007 08:04 PM

Re: Help[c++]
 
10x man 2ded ma3koun kelkoun 100/100?
:D:D
lol

Kingroudy 01-10-2007 08:09 PM

Re: Help[c++]
 
#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:D:D:D:D:D:D:D

sayo9394 01-11-2007 01:05 AM

Re: Help[c++]
 
Quote:

Originally Posted by Kingroudy
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.

Kingroudy, you 100% right. Chriseid06, you should understand my code, and try to correct your code.

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:

now a question to sayo;
double* studentArray = new double[studentNum]; <<<< what is that for????
will this is simply creating an array of type double which the "studentArray" pointer points to. if you haven't studied pointers yet, just ignore it, and do it your way.
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.. :) if not, then another way of doing the same thing is like wat i did.
cout<<"entrer un nombre qui est entre 1 et 30";
double* n= new double[x];

Quote:

#include<iostream>
using namespace std;
double m,s;
i just don't understand y you defined 2 global variables m and s?? global variables should be avoided whenever you can. in such a simple program you really shouldn't have any.
m and s should be declared in the "moyenne" function...

other than this small thing, your code is very nice and neat :) ... good stuff kingroudy.

ZeRaW 01-11-2007 01:37 AM

Re: Help[c++]
 
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.


All times are GMT +1. The time now is 07:10 PM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger