function call array function//help

hello ; check this program please , when i enter -1 to endthe program
its give me wrong grade (enter number between 0 and 100)!!
and is my way to find the maximum value between numbers input by user right ?
thanks in advance :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;

int main ()
{
      double max;
      double input;
      double grades[100]; //array to save grades
      double numbofgrades=0 ;//number of grades
      int i;
      int sum=0.0,pass=0,fail=0;
      for (i=0;i<100;i++)
      {
          cout << "Enter the grade please(-1 to end)=" ;
          cin >> grades[i];

          if (grades[i]>=0 && grades[i] <=100 && grades[i] !=-1)
            {
                sum=sum+grades[i];
                numbofgrades++;
                max=grades[0];
                for (int i=1;i<=100;i++)
                   {

                       if (grades[i]>max)
                       {
                           max=grades[i];
                       }
                   }
                if (input>=50)
                  {
                    pass++;
                  }
               else
                  {
                      fail++;
                  }
            }

           else if (input == -1)
              {

                cout << "The average of the grades=" << sum/numbofgrades ;
                cout << "\nThe number of students=" << numbofgrades ;
                cout <<"\nThe number of passing grades=" << pass ;
                cout << "\nThe number of failing grades=" << fail ;
                cout << "\nmaximum grade=" << max;
                break ;
              }

          else
            {
                 cout << "error, grade must be between (0 and 100)\n" ;
            }
      }

    return 0;
}
Last edited on
line 16 and 36: you set min and max to a[0], but a[0]] has no value yet. a[0] gets the user's input in the following loop.

i'd add another function for the input of the 10 numbers and the 12 numbers. save the input in 2 arrays. then call the min and max functions with the corresponding array and return the min, max value to print it on the screen.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;
int t,input;
double sort(int a[],int size);

int main()
{

    int array[100];

    sort(array,100);

   cout << "elements=" << input<< endl;

    return 0;
}
double sort(int a[],int size)
{
    cout << "enter the numbers please:\n" ;
    for (int i=0;i<size;i++)
    {
        for (int p=size-1;p<=i;p--)
        if(a[i]>a[p])
        {
            a[p]=input;
            a[p]=a[i];
            input=a[i];
        }
    }
    return input;
}

how can let the user enter the values (inputs) , and then the program sort the valuse from minimum to maximum ,and then print on the screen ? and why evertime i run this code , its terminate the code::blocks ?
Last edited on
thank you darkmaster for help , why no answers ppl?
why no answers ppl?


how can let the user enter the values (inputs) , and then the program sort the valuse from minimum to maximum ,and then print on the screen ?


Nobody wants to rewrite the book you should be reading. If you have problem with code that you're writing, by all means, ask for help. If you need a tutorial, find one or read your book or study material until you're able to attempt code doing what you want. If you supply no code attempting to do what you want, and then ask why you're not getting any help, it's because we put the same effort into helping you as you do into trying to code.
Hi there,

You want to put the logic for getting numbers from the user and for sorting the array in two separate functions:

1
2
3
4
5
6
7
8
void get_numbers(int a[], int size)
{
    for (int i=0; i<size; i++)
    {
         std::cout << "Enter a number please: ";
         std::cin >> a[i];
    }
}


Then call the sort function on the array after the user has filled it up.

All the best,
NwN
cire , iam rly confused , some ppl here told me if u have some problems in a code i can ask for help , if any one can find the answers in the book i would sugest to close the beginners forum .

NwN , thank you u helped me alot but we r not allowed to use the logic (std::) :) .
in your header you have said
using namespace std;

this means that you can replace std::cout and std::cin with simply cout and cin in NwN's
function.

ignore cire, there are always these guys with a bunch of posts on forums who dont want to give easy answers cause they spent a bunch of time learning stuff on their own and think people are lazy when they don't do the same - they think books and stuff first then ask people. Sure that's fine (thats what I did - been programming for over 10 years with a couple complete Apps to my name) but you have posted code and asked what's wrong with it. I see that you most likely wrote it yourself cause its beginner style coding so..

i think your question is fine here
dreamincolor , thank you for your support , i understand that book should my first refernce , but most thing in book we didnt take it with our doctor , so after i finish my exam i will study from the book and if i have any question i will ask here :) .
Topic archived. No new replies allowed.