### 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 ?
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````#include 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.
 ``12345678910111213141516171819202122232425262728293031`` ``````#include 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;ia[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?

 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:

 ``12345678`` ``````void get_numbers(int a[], int size) { for (int i=0; i> 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::) :) .
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.