Question

c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers...

c++ no pointers just follow instructions

Write a program (array.cpp) that read positive floating point numbers into an array of capacity 100. The program will then ask for one of three letters(A, M or S). if the user inputs something other than one of these letters, then the program should ask for that input until an A, M, or S is entered. A do-while loop should be used for this.

If the user inputs an A, then the program should determine and print the average of the numbers. If the user inputs an M, then the program should determine and print the max of the numbers. If the user inputs an S, then the program sorts the values into ascending order and display the numbers

Your program should define at least the following three user defined functions

1. ComputeAverage: This function computes and prints the average of the values in an array

2. FindMax: This function finds and prints the largest value in an array

3. Sort : This function sorts the values into ascending order

copy the datafile into the project directory using the command :

cp ~mnan/data/arraytest.dat

The program should include all directives and comments

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM

#include<iostream>
using namespace std;
//function to find average
int ComputeAverage(float a[100])
{
float sum=0,avg;
//using a loop, each element is added to the sum
for(int i=0;i<100;i++)
{
sum=sum+a[i];
}
//average is found by dividing the total sum to the no:of elements
avg=sum/100;
//printing result
cout<<"Average is "<<avg;
return 0;
}
//function to find max of nos
int FindMax(float a[100])
{
int i;
float n=0;
//using a loop check whether the particular element of the array is greater than the variable n
for(i=0;i<100;i++)
{
if(a[i]>n)
//if greater, assign to n.
n=a[i];
}
//print max
cout<<"Max of the numbers are: "<<n;
return 0;
}
//function to sort the array
int Sort(float a[100])
{
float temp;
int i,j;
//using 2 loops check if the first element is greater than the second.
for(i=0; i<100; i++)
{
for(j=i+1; j<100; j++)
{
if(a[j] < a[i])
{
//if greater,swap the numbers
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
//display sorted array
cout<<"The sorted array:\n";
for(i=0;i<100;i++)
{
cout<<endl<<a[i];
}   
return 0;
}
int main()
{
float arr[100];
int i;
char c;
cout<<"Enter the numbers: \n";
//read elements of the array
for(i=0;i<100;i++)
{
cin>>arr[i];
}
//prompt for user selection
cout<<"A , M or S ? :\n";
do
{
//read user input
cin>>c;
//using switch, check user input
switch(c)
{
case 'A':
//if A , call ComputeAverage()
ComputeAverage(arr);
return 0;
break;
case 'M':
//if M call FindMax()
FindMax(arr);
return 0;
break;
case 'S':
//if S call Sort()
Sort(arr);
return 0;
break;
default:
//if user enters wrong input, prompt for correct input.
cout<<"\n Please choose one of these. A , M or S?\n";
}
//do this loop until user enters valid input.
}while(c!='A'&& c!='M' && c!='S');
  
return 0;   
}
  1#include<iostream> 2 using namespace std; 3//function to find average 4 int ComputeAverage(float a[100]), 5{ 6. float sum=0,22 float n=0; 231 //using a loop check whether the particular element of the array is greater than the variable n for(i=0; i<{ for(j=i+1; j<100; j++), if(a[j] = a[i]) //if greater, swap the numbers temp = a[i]; a[i] = a[j]; a[j] = temp; 1 7/display64 65 float arr[100]; int i; char c; cout<<Enter the numbers: \n; //read elements of the array, for(i=0; i<100; i++) 66 cibreak; case M: //if Mcall FindMax (). FindMax(arr); return 0; break; case S: //if S call Sort() Sort(arr); return 0; brea

SAMPLE OUTPUT( TO CHECK THE OUTPUT, I USED ONLY 5 ELEMENTS FOR THE ARRAY, NOT 100, AS IT TAKES A LOT OF TIME)

Enter the numbers: 1.1. 2.1. A , M or s ? : Average is 3.04Enter the numbers: 3.2 4.6 1.3 0.9 2.1. A , M or s ? : Max of the numbers are: 4.6Enter the numbers: 4.3 1.2 9.3 2.4 0.3 A , Mor S ? : S The sorted array: 0.3 1.2 2.4 4.3 9.3Enter the numbers: 4.2 4.3 6.5 9.2 A , M or ? : Please choose one of these. A , M or S? Please choose one of these. A , Mor S

All are 4 different runs.

I hope this is what you are looking for. Please rate this answer to let me know it was helpful to you. Thanks for reading. If any doubts please mention it in the comments below.

Add a comment
Know the answer?
Add Answer to:
c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • LANGUAGE: JAVA Sorting an array: 2. Write a program that asks the user for 5 numbers...

    LANGUAGE: JAVA Sorting an array: 2. Write a program that asks the user for 5 numbers and stores them in an array called data. Then call a method that accepts the array as an argument, sorts the array in ascending order and prints out the original AND sorted array (Hint: The method does not return anything in this case, so it should be set to void).

  • C++ Problems: 1. Write a program that reads in an array of user given size n and then it reads se...

    C++ Problems: 1. Write a program that reads in an array of user given size n and then it reads several increment values terminated by 0. The program calls each time a function incarray() that accepts an array A, its size S and an increment value inc and updates yje array by incrementing all its values by the passed value of inc. The program prints out the array after each update. 2. write a program that reads in a n...

  • read it carefully C++ Question: Write a program that dynamically allocates an array large enough to...

    read it carefully C++ Question: Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible. Input Validation: Do not...

  • This program is in C++ Write a program that validates charge account numbers read in from...

    This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...

  • In Python 3, Write a program that reads a set of floating-point values. Ask the user...

    In Python 3, Write a program that reads a set of floating-point values. Ask the user to enter the values, then print: The number of values entered. The smallest of the values The largest of the values. The average of the values. The range, that is the difference between the smallest and largest values. If no values were entered, the program should display “No values were entered” The program execution should look like (note: the bold values are sample user...

  • Question: In C++ Write a correct and complete C++ program th... Bookmark In C++ Write a...

    Question: In C++ Write a correct and complete C++ program th... Bookmark In C++ Write a correct and complete C++ program that inputs 20 integer values from the user and performs three calculations. In the main function, input the values from the user. As part of your answer, write a sub-function, named calcAvg (), which takes two arguments and returns the average of all the integers. Also write another sub-function, named reverseArray (), which takes two arguments and reverses the...

  • In C++ This program will read a group of positive numbers from three files ( not...

    In C++ This program will read a group of positive numbers from three files ( not all necessarily the same size), and then calculate the average and median values for each file. Each file should have at least 10 scores. The program will then print all the scores in the order that they were input, showing each number and what percentage it is above or below the average for each file. Note: Finding the average doesn’t require an array. Finding...

  • Write a C PROGRAM that will read in 10 floating-point numbers from the keyboard and load...

    Write a C PROGRAM that will read in 10 floating-point numbers from the keyboard and load them into an array. Add up all the values and store the answer in a variable. Find the largest and smallest number in the array. Put each into its own variable. Print to the screen the sum, largest value, and smallest value. Copy the array to a second array in reverse order. Print out the second array. Read in 20 integer numbers, all in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT