data.txt
I was able to read that number from data.txt
and output that number, but I don't know how to create a program that prints the maximum and minimum values.
By the way, data.txt
has
23 12 17 67 45 59 12 96 3387
If you implement a program like this, the results of the execution will be
$./a.out
23 12 17 67 45 59 12 96 33 87
appears.
#include<stdio.h>
constint N = 10;
int main()
{
FILE*fp;
inti, data [N];
char fname[] = "data.txt";
if(((fp=fopen(fname, "r")))==NULL){
printf("You cannot open it.\n");
return-1;
}
i = 0;
while(i<N){
fscanf(fp, "%d", & data[i]);
printf("%5d", data[i]);
i++;
}
printf("\n");
fclose(fp);
}
Where and what statements should I add to output the maximum and minimum values?
You can write the program itself to find the minimum or maximum value, but I don't know how to add it to the program above and create a program that meets the requirements.
Below is a program that finds the minimum and maximum values.
Program for minimum value
#include<stdio.h>
void readIntArray(inta[], int size)
{
inti;
for(i=0;i<size;i=i+1){
printf("%dth?", i+1);
scanf("%d", & a[i]);
}
}
/* Output an array containing size data from the beginning of the array */
void printIntArray(inta[], int size)
{
inti;
for(i=0;i<size;i=i+1){
printf("%d", a[i]);
}
printf("\n");
}
// Returns the minimum value in the integer array a of element number sizes.
int minIntArray(inta[], int size)
{
inti,min;
min = a[0];
for(i=1;i<size;i=i+1){
if(a[i]<min){
min = a[i];
}
}
return min;
}
int main (void)
{
int data [100], size, minv;
printf("Please enter the number of data:");
scanf("%d", & size);
readIntArray(data,size);
printf("Output in order:");
printIntArray(data,size);
minv = minIntArray(data,size);
printf("Minimum value is %d.\n", minv);
return 0;
}
Program for maximum value
#include<stdio.h>
void readIntArray(inta[], int size)
{
inti;
for(i=0;i<size;i=i+1){
printf("%dth?", i+1);
scanf("%d", & a[i]);
}
}
/* Output an array containing size data from the beginning of the array */
void printIntArray(inta[], int size)
{
inti;
for(i=0;i<size;i=i+1){
printf("%d", a[i]);
}
printf("\n");
}
int maxIntArray (inta[], int size)
{
inti,max;
max = a[0];
for(i=1;i<size;i=i+1){
if(a[i]>max){
max = a[i];
}
}
return max;
}
int main (void)
{
int data [100], size, maxv;
printf("Please enter the number of data:");
scanf("%d", & size);
readIntArray(data,size);
printf("Output in order:");
printIntArray(data,size);
maxv = maxIntArray(data,size);
printf("Maximum value is %d.\n", maxv);
return 0;
}
I saw other questions.Similar things were pointed out there, but
I think it's good to be aware that
For procedural languages such as C, the order of processing is important.
For the C language, the main function is the program body.For Presentation Codes,
int main()
{
FILE*fp;
inti, data [N];
char fname[] = "data.txt";
// at this point
At this point, fp, i, data[0]~data[N-1] contain a very bad number and
The fname contains data.txt.
if((fp=fopen(fname, "r"))==NULL){
runs from within.
Consider a case where file opening did not fail.
if((fp=fopen(fname, "r"))==NULL){
// This place has been skipped.
}
// here
}
where fp
contains a file pointer and fname
contains "data.txt" and
i
, data[0]
through data[9]
contain an awful number.
if((fp=fopen(fname, "r"))==NULL){
// skipping
}
i = 0;
// here
}
where fp
contains a file pointer, i
contains 0, fname
contains "data.txt", and data[0]
through data[9]
contains a terrible number.
while(i<N){
Run in {}
while is i<N.
In the first run, i = 0, so
fscanf(fp, "%d", & data[i]);
printf("%5d", data[i]);
i++;
Now
In the next run, i
is 1, so store it in data[1]
.
The while statement no longer satisfies i<N
immediately after processing when i
is 9, and
i=0;
while(i<N){
fscanf(fp, "%d", & data[i]);
printf("%5d", data[i]);
i++;
}
// here
printf("\n");
fclose(fp);
}
I'm going to fly here.
Currently fp
contains a file pointer, i
contains 10, data[0]
through data[9]
read from the file, and fname
contains "data.txt".
Then print a new line with printf("\n";
and close the file with fclose(fp);
.
Now that the main function is over, the program will exit.
Now, minIntArray
seems to need to pass an array of minimum value candidates and their number as arguments.
Now that you've written //here
, there should be a point where the array contains numbers that you read from a file that are not crazy numbers.
Insert minIntArray
to find the minimum value, and minIntArray
to store the minimum value.(The minimum variable
must be declared separately at the top of the function.)
You can use the debugger to see what I said above, "The value of this variable is ~."
I think you're using gcc or something in your class, but if you're not used to using the CLI, I recommend you to introduce IDE personally.
© 2024 OneMinuteCode. All rights reserved.