how to pass an array into an function and in the function count how many numbers are in a range? [closed]
#include <iostream>
#include <fstream>
using namespace std;
int calculate_total(int exam1[], int exam2[], int exam3[]); // function that calcualates grades to see how many 90,80,70,60
int exam1[100];// array that can hold 100 numbers for 1st column
int exam2[100];// array that can hold 100 numbers for 2nd column
int exam3[100];// array that can hold 100 numbers for 3rd column
// here i am passing an array into the function calcualate_total
int calculate_total(exam1[],exam2[],exam3[])
{
int above90=0, above80=0, above70=0, above60=0;
if((num<=90) && (num >=100))
{
above90++;
{
if((num<=80) && (num >=89))
{
above80++;
{
if((num<=70) && (num >=79))
{
above70++;
{
if((num<=60) && (num >=69))
{
开发者_如何学Pythonabove60++;
}
}
}
}
}
}
}
}
Instead of using nested if. Use if, else if and else. Your program will be clean and will help others read better.
Looks like you are trying to create a frequency table.
Unfortunately, the code for the logic is wrong.
When you use &&
AND
It means it must satisfy both conditions.
How can a number be less than or equal to 90 and greater than or equal to a 100 at the same time?
May be what you are looking for is ||
which is OR.
And finally you need a loop.
By the pointer to int array. (here is definition)
int calculate_total(int *exam1, int *exam2, int *exam3)
If you want call this function, you must in each argument push the address of examX array starting. If you want get element, you must add to the staring array address, a element offset address and get value from her.
use a vector. you can initialize a vector like an array. the vector has a method to give you the number of elements
精彩评论