Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
I am working on a program and im trying to figure out the logic and it is not making any sense?
I can not figure out how to find the highest and lowest of the array in a function
// This program will read in a group of test scores( positive integers from 1 to 100)
// from the keyboard and then calculates and outputs the average score
// as well as the highest and lowest score. There will be a maximum of 100 scores.
// Meredith Martin
#include <iostream>
using namespace std;
typedef int GradeType[100]; // declares a new data type:
// an integer array of 100 elements
float findAverage (const GradeType, int); // finds average of all grades
int findHighest (const GradeType, int); // finds highest of all grades
int findLowest (const GradeType, int); // finds lowest of all grades
int main()
{
GradeType grades; // the array holding the grades.
int numberOfGrades; // the number of grades read.
int pos; // index to the array.
float avgOfGrades; // contains the average of the grades.
int highestGrade; // contains the highest grade.
int lowestGrade; // contains the lowest grade.
// Read in the values into the array
pos = 0;
cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
cin >> grades[pos];
while (grades[pos] != -99)
{
pos++; //changes array number
cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
cin >> grades[pos];
}
numberOfGrades = pos - 1; // Fill blank with appropriate identifier
// call to the function to find average
avgOfGrades = findAverage(grades, numberOfGrades);
cout << endl << "The average of all the grades is " << avgOfGrades << endl;
highestGrade = findHighest(grades, numberOfGrades);
cout << endl << "The highest grade is " << highestGrade << endl;
lowestGrade = findLowest(grades, numberOfGrades);
cout << endl << "The lowest grade is " << lowestGrade << endl;
system("PAUSE");
return 0;
}
//****************************************************************************
// findAverage
//
// task: This function receives an array of integers and its size.
// It finds and returns the average of the numbers in the array
// data in: array of floating point numbers
// data returned: avarage of the numbers in the array
//
//****************************************************************************
float findAverage (const GradeType array, int size)
{
float sum = 0; // holds the sum of all the numbers
for (int pos = 0; pos < size; pos++)
sum = sum + array[pos];
return (sum / size); //returns the average
}
//****************************************************************************
// findHighest
//
// task: This function receives an array of integers and its size.
// It finds and returns the highest value of the numbers in
// the array
// data in: array of floating point numbers
// data returned: highest value of the numbers in the array
//
//****************************************************************************
int findHighest (const GradeType array, int size)
{
float highest = 0;
for(int pos = 0; pos < size; pos++)
{
if (array[pos] >= highest)
highest = array[pos];
}
}
//****************************************************************************
// findLowest
//
// task: This function receives an array of integers and its size.
// It finds and returns the lowest value of the numbers in
// the array
// data in: array of floating point numbers
// data returned: lowest value of the numbers in the array
//
//****************************************************************************
int findLowest (const GradeType array, int size)
{
float lowest = -1;
for(int pos = 0; pos < size; pos++)
{
if (array[pos] <= lowest)
lowest = array[pos];
}
}
1 Answer
- 1 decade ago
im not good at c more into VB but seems like the code is already there
you need to loop through all the grades if the current grade is higher then the current highest then reset the highest value to the current grade and same with the lowest in VB it would be something like
for i = 1 to 100 'assuming 100 gradse
if grade(i) => highest then highest = grade(i)
if grade(i) =< lowest then lowest = grade(i)
next i
you can find the higest and lowest at the same time... faster then doing two 100 loops... i hope that helps
Source(s): my butt