Question

Problem Definition Write a program called process_ages that reads in an array of up to 10...

Problem Definition

Write a program called process_ages that reads in an array of up to 10 integers representing peoples ages in years and calculates and prints the maximum age and the minimum age and the average age. Your program should stop reading when either the 10th number is entered or the user enters -1. If the user enters any other negative number or a value above 150, they should be warned and prompted again for a valid age.

You should then print the:

  1. maximum
  2. minimum and
  3. average

ages on the same line.

Testing
In a text file called process_ages.txt write three test cases. Two example test cases are:

input        maximum  minimum  average
10 11 12     12       10       11
1 2 3 4      4        1        2.5

Coding

Implement the problem definition above in a file called process_ages.c.

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

Please see the code below, it has appropriate comments

/******************************************************************************

Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>
#include <stdlib.h>

int main()
{
int count=0,min=0,max=0,num[10],i,j,temp,temp1,size=0,sum=0;
float avg;
// Take input of ten numbers
printf("Enter 10 numbers:\n");
for(i=0;i<=9;i++)
{
scanf("%d",&temp);
if(temp==-1) //check if input is -1
{
size=i;
goto X;}
else if(temp>150 && temp < -1)
printf("enter either -1 or a less positive number"); //Warning to enter correct values
else
{
num[i]=temp;
temp=0;
}
i=i++;
}
  
X:
if(size==0)
{
printf("No data given");
return 0;
}
for (j=0;j<size;j++) //calculating sum
sum=num[j]+sum;
avg=sum/(float)size; //calcuating average
  
if (size>2)
{
//sorting numbers in ascending order
for(i=0;i<=size;i++)
{
for(j=0;j<=size;j++)
{
if(num[j]>num[j+1])
{
temp1=num[j];
num[j]=num[j+1];
num[j+1]=temp1;
}
}
}
}
  
printf("\n The Maxmimum number: %d",num[size]); //print Maxmimum
printf("\n The Minimum number: %d", num[1]); //print Minimum
printf("\n The average: %f",avg); //print average
  

return 0;
}

*****************************************

one sample of output with this code

Add a comment
Know the answer?
Add Answer to:
Problem Definition Write a program called process_ages that reads in an array of up to 10...
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
  • Description: Create a program called numstat.py that reads a series of integer numbers from a file...

    Description: Create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the name of file, sum of numbers, count of numbers, average of numbers, maximum value, minimum value, and range of values. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information. Requirements: Create a program called numstat.py that reads a series of integer numbers from a file and determines...

  • Write a java program that reads a given data file called tickets that consists of tickets...

    Write a java program that reads a given data file called tickets that consists of tickets and the ticket prices. Your program should read the data and compute the find minimum, maximum and average ticket prices and output the report into a file called output.txt. The report file should look exactly (or better than) the one shown below. Your program should implement the Java ArrayList class, Java File I/O, and Java error handling exception. Below is the content of the...

  • Matlab Question 2. For this problem you have to create a program that defines two vectors...

    Matlab Question 2. For this problem you have to create a program that defines two vectors of numbers: A and B and uses a for-loop to combine the values of A and B. Note that different parts of the question below specify different orders for combining the elements. To start this exercise start a MATLAB script called q2.m and put, at the top, the vector definition: B [11:20] Question 2a: Problem definition Copy the file q2.m to q2a.m. For this...

  • General Requirements . . . Write a program that reads letters from a file called "letters.txt"....

    General Requirements . . . Write a program that reads letters from a file called "letters.txt". Your program will open the letters.txt file read in one character at a time For this assignment the test file will contain at least 30 letters, uppercase OR lowercase In your program you will change each letter (solution) to uppercase Use the function toupper in #include <ctype.h> You create a numerical version of the uppercase letter from the file . o Use int numberSolution...

  • 1. Write a program called Numbers that a. prompts the user for a file name. b....

    1. Write a program called Numbers that a. prompts the user for a file name. b. reads that file assuming that its contents consist entirely of integers. c. prints the maximum, minimum, sum, count (number of integers in the file), and average of the numbers. For example, if the file numberinput.dat has the following content: 4 -2 18 15 31 27 Your program should produce the following output: csc% java Numbers Enter file name: numberinput.daft Maximum31 Minimum- -2 Sum -...

  • In Python, write a program that reads a text file that is provided by the user...

    In Python, write a program that reads a text file that is provided by the user - ensure that the file exists before processing it! The file might be in a different directory, so be sure to test you logic - the user will provide the absolute path to the file if it is not in the same directory as the Python script! You should then ask the user for what string to search for in the file. Your program...

  • //Done in C please! //Any help appreciated! Write two programs to write and read from file...

    //Done in C please! //Any help appreciated! Write two programs to write and read from file the age and first and last names of people. The programs should work as follows: 1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0, the...

  • I need to Write a test program that prompts the user to enter 10 double values...

    I need to Write a test program that prompts the user to enter 10 double values into an array, calls a method to calculate the average of the numbers, and displays the average. Next, write a method that returns the lowest value in the array and display the lowest number. The program should then prompt the user to enter 10 integer values into an array, calculates the average, and displays the average. The program should have two overloaded methods to...

  • using matlab 3. Write a script that will read the ages vector.dat file created in the...

    using matlab 3. Write a script that will read the ages vector.dat file created in the previous problem. - the script will print the number of persons in the group - the script will print the maximum and minimum ages in the group - the script will compute and print the "average age" The script should return something like: The file contains the ages of a group of 50 people The maximum and minimum ages in the group are 10...

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