Question#1: Write a C program that prints out the max and min values of the data
ID: 3687297 • Letter: Q
Question
Question#1: Write a C program that prints out the max and min values of the data file included below. Instead of using computations within the main function like the problem in previous homework, use the programmer-define functions (reference functions with function prototypes) to solve it. You can choose anyone of the three data file structures (counter, sentinel, and neither counter/sentinel) to be included in your solution.
Program #1
130.5
140.4
140
150
160.9
170
180
150
150.1
190
Program #2
120
140.8
121
-160.9
130.4
190.4
150.6
110
170.7
180
Program #3
140
150.2
160.3
170
180
190
-110
120.1
130
170
Question#2: Write a C program that generates 50 floating-point random numbers in a range from 0 to 10,000 using different seed values. Five floating-point random numbers per line are required to be displayed on the output.
Question#3: Write a C program that generates 40 integer random numbers in a range from 0 to 1,000 using different seed values. Four integer random numbers per line are required to be displayed on the output.
***turn in the computer print outs of the C programs, and the compiling steps.
Explanation / Answer
#1)
/**C program that reads a text file "input.txt"
and prints maximum and minimum values to console*/
#include<stdio.h>
#include<conio.h>
//Set constant
const int SIZE =10;
//functions prototypes
void readFile(FILE* fp,double arr[]);
double getMax(double arr[]);
double getMin(double arr[]);
int main()
{
FILE *fp;
fp=fopen("input.txt","r");
if(!fp)
{
printf("File doesn't exist. ");
getch();
return 0;
}
double arr[SIZE];
//calling readFile
readFile(fp,arr);
//calling getMax
printf("Maximum : %5.2f ",getMax(arr));
//calling getMin
printf("Minimum : %5.2f ",getMin(arr));
//pause the program output on console
getch();
return 0;
}
//The method readFile that takes FILE pointer and array
//and read the array values
void readFile(FILE* fp,double arr[])
{
int index=0;
while(index<SIZE)
{
fscanf(fp,"%lf",&arr[index]);
index++;
}
fclose(fp);
}
//Returns the maximum value in array
double getMax(double arr[])
{
double max=arr[0];
int index;
for(index=1;index<SIZE;index++)
{
if(arr[index]>max)
max=arr[index];
}
return max;
}
//Returns the minimum value in array
double getMin(double arr[])
{
double min=arr[0];
int index;
for(index=1;index<SIZE;index++)
{
if(arr[index]<min)
min=arr[index];
}
return min;
}
Sample output:
Maximum : 190.00
Minimum : 130.50
----------------------------------------------------------------------------------------------------
#2)
/**C program that generates 50 random numbers in a range of 0 to 10000
and print to console.*/
//header files
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
//Set constant
const int SIZE =50;
//functions prototypes
int main()
{
//create an array of size 50
double arr[SIZE];
//seed a random value
srand(time_t(NULL));
int index;
int count=1;
for(index=0;index<SIZE;index++)
{
arr[index]=rand()%10000;
}
//print 4 random numbers per line
for(index=0;index<SIZE;index++)
{
printf("%10.2f",arr[index]);
if(count%5==0)
printf(" ");
count++;
}
//pause the program output on console
getch();
return 0;
}
sample output:
38.00 7719.00 1238.00 2437.00 8855.00
1797.00 8365.00 2285.00 450.00 612.00
5853.00 8100.00 1142.00 281.00 537.00
5921.00 8945.00 6285.00 2997.00 4680.00
976.00 1891.00 1655.00 5906.00 8457.00
1323.00 8881.00 2240.00 9725.00 2278.00
2446.00 590.00 840.00 8587.00 6907.00
1237.00 3611.00 2617.00 2456.00 867.00
9533.00 6878.00 8223.00 7887.00 1597.00
584.00 2212.00 1111.00 7578.00 7066.00
------------------------------------------------------------------------------------------------
#3)
/**C program that generates 40 random numbers in a range of 0 to 1000
and print 4 random numbers per line to console.*/
//header files
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
//Set constant
const int SIZE =40;
//functions prototypes
int main()
{
//create an array of size 50
int arr[SIZE];
//seed a random value
srand(time_t(NULL));
int index;
int count=1;
for(index=0;index<SIZE;index++)
{
arr[index]=rand()%1000;
}
//print 4 random numbers per line
for(index=0;index<SIZE;index++)
{
printf("%10d",arr[index]);
if(count%4==0)
printf(" ");
count++;
}
//pause the program output on console
getch();
return 0;
}
sample output:
38 719 238 437
855 797 365 285
450 612 853 100
142 281 537 921
945 285 997 680
976 891 655 906
457 323 881 240
725 278 446 590
840 587 907 237
611 617 456 867
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.