Question#1: Write a C program that prints out the max and min values of the data
ID: 3686750 • 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
Program 1
#include <stdio.h>
#define MAX 10
void getMinMax(float dataArr[10],float *min,float *max)
{
*min=dataArr[0];
*max=dataArr[0];
int i;
for(i = 1; i < MAX; i++)
{
if(*min>dataArr[i])
*min=dataArr[i];
if(*max<dataArr[i])
*max=dataArr[i];
}
}
int main(void)
{
FILE *myfile;
float min,max;
float myvariable;
float dataArr[10];
int i;
myfile=fopen("data.txt", "r");
for(i = 0; i < MAX; i++)
{
fscanf(myfile,"%f",&dataArr[i]);
printf("%f ",dataArr[i]);
}
getMinMax( dataArr,&min,&max);
printf(" Minimum:%.2f Maximum:%.2f ",min,max);
fclose(myfile);
}
OUTPUT:
130.500000
140.399994
140.000000
150.000000
160.899994
170.000000
180.000000
150.000000
150.100006
190.000000
Minimum:130.50
Maximum:190.00
Process returned 0 (0x0) execution time : 0.034 s
Press any key to continue.
Program 2:
#include <stdio.h>
int main()
{
srand((unsigned int)time(NULL));
int i;
for ( i=1; i<=50; i++)
if(i%5==0)
{
printf("%.2f ", ((float)rand()/(float)(10000)));
}
else
printf("%.2f ", ((float)rand()/(float)(10000)));
}
OUTPUT:
0.85 0.26 2.53 0.91 1.19
2.32 0.34 1.26 1.22 2.38
2.51 2.27 0.86 1.91 2.50
2.00 0.01 0.83 3.25 1.46
0.52 0.23 2.94 0.25 0.00
3.24 2.51 2.12 1.84 0.30
1.23 1.00 1.60 1.94 0.20
1.82 0.55 0.46 3.16 0.71
3.17 2.29 1.72 1.70 0.12
2.50 3.03 1.34 1.84 0.72
Process returned 5 (0x5) execution time : 0.034 s
Press any key to continue.
Program 3:
#include <stdio.h>
int main()
{
srand((unsigned int)time(NULL));
int i;
for ( i=1; i<=40; i++)
if(i%4==0)
{
printf("%d ", (rand()/(1000)));
}
else
printf("%d ", (rand()/(1000)));
}
OUTPUT:
9 3 17 21
31 20 15 1
19 12 30 30
29 32 22 14
25 5 7 26
29 17 24 8
18 24 25 21
12 30 4 11
9 7 2 5
21 17 17 20
Process returned 3 (0x3) execution time : 0.041 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.