Question

Use two files for this lab: your C program file, and a separate text file containing...

Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from the file and place into the next available position of an array. When all values have been read and saved in the array, other functions will be called to perform the processing tasks on the array.

#include <stdio.h>

// Define other functions here to process the filled array: average, max, min, sort, search

// Define computeAvg here

// Define findMax here

// Define findMin here

// Define selectionSort here ( copy from zyBooks 11.6.1 )

// Define binarySearch here


int main(void) {

// Declare variables
FILE* inFile = NULL; // File pointer
int singleNum; // Data value read from file
int valuesRead; // Number of data values read in by fscanf
int counter=0; // Counter of values read and saved in array
int arrOfValues[50]; // Array to store values read in from file
double avgOfArray; // Average of all values saved in the array
int maxValue; // Largest value in the array
int minValue; // Smallest value in the array
int i;

// Try to open file
printf("Opening file myfile2.txt.\n");
inFile = fopen("myfile2.txt", "r");

if (inFile == NULL) {
printf("Could not open file myfile.txt.\n");
return -1; // -1 indicates error
}

// Print read numbers to output
printf("Reading and printing numbers.\n");

while (!feof(inFile)) {
valuesRead = fscanf(inFile,"%d", &singleNum);
if ( valuesRead == 1 ) {
printf("value read in was:%d\n", singleNum);
arrOfValues[counter] = singleNum;
counter++;
}
}

printf("Closing file myfile.txt.\n");

// Done with file, so close it
fclose(inFile);

/* Below, call each function defined above main and display the returned result. For selectionSort function, it will not return a value, but will sort the values in the array. After calling selectionSort function, use a loop to display all the values in the array to verify they have been sorted. After call the binarySearch function, test the returned value and either display a message stating that the value could not be found, or used the returned value to access and display the value. */


return 0;
}

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

Please comment, if you need any corrections.Please give a Thumps up if you like the answer.

Program

#include <stdio.h>

// Define other functions here to process the filled array: average, max, min, sort, search

// Define computeAvg here
float computeAvg(int arrOfValues[],int size)
{
   float sum=0;
   for(int i=0;i<size;i++)
   {
       sum+=arrOfValues[i];
   }
  
   return(sum/size);
}
// Define findMax here
int findMax(int arrOfValues[],int size)
{
   int largest=arrOfValues[0];
   for(int i=0;i<size;i++)
   {
       if(arrOfValues[i]>largest)
       largest=arrOfValues[i];
   }
  
   return(largest);
}
// Define findMin here
int findMin(int arrOfValues[],int size)
{
   int smallest=arrOfValues[0];
   for(int i=0;i<size;i++)
   {
       if(arrOfValues[i]<smallest)
       smallest=arrOfValues[i];
   }
  
   return(smallest);
}

// Define selectionSort here ( copy from zyBooks 11.6.1 )
void selectionSort(int arrOfValues[], int n)
{
   int i, j, min,temp;

  
   for (i = 0; i <= n-2; i++)
   {
       // Find the minimum element in unsorted array
       min = i;
       for (j = i+1; j <=n-1; j++)
       if (arrOfValues[j] < arrOfValues[min])
           min = j;
      
       if(min!=i)
       {
       // Swap the found minimum element with the first element
       temp=arrOfValues[min];
       arrOfValues[min]=arrOfValues[i];
       arrOfValues[i]=temp;
       }
   }
}


// Define binarySearch here
int binarySearch(int arrOfValues[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arrOfValues[mid] == x)
return mid;
if (arrOfValues[mid] > x)
return binarySearch(arrOfValues, l, mid - 1, x);
return binarySearch(arrOfValues, mid + 1, r, x);
}
return -1;
}

int main(void) {

// Declare variables
FILE* inFile = NULL; // File pointer
int singleNum; // Data value read from file
int valuesRead; // Number of data values read in by fscanf
int counter=0; // Counter of values read and saved in array
int arrOfValues[50]; // Array to store values read in from file
double avgOfArray; // Average of all values saved in the array
int maxValue; // Largest value in the array
int minValue; // Smallest value in the array
int i,key;

// Try to open file
printf("Opening file myfile2.txt.\n");
inFile = fopen("myfile2.txt", "r");

if (inFile == NULL) {
printf("Could not open file myfile.txt.\n");
return -1; // -1 indicates error
}

// Print read numbers to output
printf("Reading and printing numbers.\n");

while (!feof(inFile)) {
valuesRead = fscanf(inFile,"%d", &singleNum);
if ( valuesRead == 1 ) {
printf("value read in was:%d\n", singleNum);
arrOfValues[counter] = singleNum;
counter++;
}
}

printf("Closing file myfile.txt.\n");

// Done with file, so close it
fclose(inFile);
// Call computeAvg
avgOfArray=computeAvg(arrOfValues,counter);
printf("Average=%.2f\n",avgOfArray);
maxValue=findMax(arrOfValues,counter);
printf("Maximum =%d\n",maxValue);
minValue=findMin(arrOfValues,counter);
printf("Minimum =%d\n",minValue);
selectionSort(arrOfValues,counter) ;
printf("\nSorted Values: \n");
for(int i=0;i<counter;i++)
{
printf("%d\t",arrOfValues[i]);
}
printf("\nEnter key element to search: ");
scanf("%d",&key);
int result=binarySearch(arrOfValues,0,counter,key);
if(result==-1)
   printf("\n%d not found.\n",key);
else
   printf("\n%d found at position %d.\n",key,result);
return 0;
}


Output 1

Opening file myfile2.txt.
Reading and printing numbers.
value read in was:123
value read in was:52
value read in was:112
value read in was:130
value read in was:12
value read in was:101
value read in was:121
value read in was:19
value read in was:120
value read in was:9
value read in was:109
value read in was:211
value read in was:101
value read in was:110
value read in was:113
value read in was:900
value read in was:102
value read in was:510
value read in was:129
value read in was:131
value read in was:923
value read in was:210
value read in was:75
value read in was:85
value read in was:92
value read in was:81
value read in was:80
value read in was:74
value read in was:7
value read in was:79
value read in was:18
value read in was:17
value read in was:62
value read in was:56
value read in was:37
value read in was:46
value read in was:76
value read in was:25
value read in was:53
value read in was:63
value read in was:62
value read in was:95
value read in was:365
value read in was:632
value read in was:512
value read in was:690
value read in was:86
value read in was:101
value read in was:116
value read in was:184
Closing file myfile.txt.
Average=164.34
Maximum =923
Minimum =7

Sorted Values:
7   9   12   17   18   19   25   37   46   52   53   56   62   62   63   74   75   76   79   80   81   85   86   92   95   101   101   101   102   109   110   112   113   116   120   121   123   129   130   131   184   210   211   365   510   512   632   690   900   923  
Enter key element to search: 120

120 found at position 34.

Output 2
Opening file myfile2.txt.
Reading and printing numbers.
value read in was:123
value read in was:52
value read in was:112
value read in was:130
value read in was:12
value read in was:101
value read in was:121
value read in was:19
value read in was:120
value read in was:9
value read in was:109
value read in was:211
value read in was:101
value read in was:110
value read in was:113
value read in was:900
value read in was:102
value read in was:510
value read in was:129
value read in was:131
value read in was:923
value read in was:210
value read in was:75
value read in was:85
value read in was:92
value read in was:81
value read in was:80
value read in was:74
value read in was:7
value read in was:79
value read in was:18
value read in was:17
value read in was:62
value read in was:56
value read in was:37
value read in was:46
value read in was:76
value read in was:25
value read in was:53
value read in was:63
value read in was:62
value read in was:95
value read in was:365
value read in was:632
value read in was:512
value read in was:690
value read in was:86
value read in was:101
value read in was:116
value read in was:184
Closing file myfile.txt.
Average=164.34
Maximum =923
Minimum =7

Sorted Values:
7   9   12   17   18   19   25   37   46   52   53   56   62   62   63   74   75   76   79   80   81   85   86   92   95   101   101   101   102   109   110   112   113   116   120   121   123   129   130   131   184   210   211   365   510   512   632   690   900   923  
Enter key element to search: 2

2 not found.

myFile.txt

123
52
112
130
12
101
121
19
120
9
109
211
101
110
113
900
102
510
129
131
923
210
75
85
92
81
80
74
7
79
18
17
62
56
37
46
76
25
53
63
62
95
365
632
512
690
86
101
116
184

Add a comment
Know the answer?
Add Answer to:
Use two files for this lab: your C program file, and a separate text file containing...
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
  • #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort,...

    #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort, search // Define computeAvg here // Define findMax here // Define findMin here // Define selectionSort here ( copy from zyBooks 11.6.1 ) // Define binarySearch here int main(void) { // Declare variables FILE* inFile = NULL; // File pointer int singleNum; // Data value read from file int valuesRead; // Number of data values read in by fscanf int counter=0; // Counter of...

  • I need help with the following code... Instructions: This lab builds on the skills from Lab...

    I need help with the following code... Instructions: This lab builds on the skills from Lab 2c, in which you read data values from a file and kept a count of the number of invalid values. In this lab, your program will be using the same code to read each data entry from the file, but you will also save each value in an array named allMags after each is read in. When all values in the file have been...

  • This lab is to give you more experience with C++ Searching and Sorting Arrays Given a...

    This lab is to give you more experience with C++ Searching and Sorting Arrays Given a file with data for names and marks you will read them into two arrays You will then display the data, do a linear search and report if found, sort the data, do a binary search. Be sure to test for found and not found in your main program. Read Data Write a function that reads in data from a file using the prototype below....

  • For this c++ assignment, Overview write a program that will process two sets of numeric information....

    For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....

  • Please help in C: with the following code, i am getting a random 127 printed in...

    Please help in C: with the following code, i am getting a random 127 printed in front of my reverse display of the array. The file i am pulling (data.txt) is: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 when the reverse prints, it prints: 127 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 not sure why...please...

  • please complete the missing function only to figure out how many numbers fall within the range...

    please complete the missing function only to figure out how many numbers fall within the range of 90 through 99 total of 29 values in C 6 finclude "lab5.h" 8 const char *FILENAME() - / array of the data file names * ("lab5a.dat", "lab5b.dat", NULL); 12 int main(void) 13 int file count = 0; keeps track of which file we are on/ int check overflow - 0; / counter to prevent array overflow int real filesize = 0; /actual count...

  • Update your first program to dynamically allocate the item ID and GPA arrays. The number of...

    Update your first program to dynamically allocate the item ID and GPA arrays. The number of items will be the first number in the updated “student2.txt” data file. A sample file is shown below: 3 1827356 3.75 9271837 2.93 3829174 3.14 Your program should read the first number in the file, then dynamically allocate the arrays, then read the data from the file and process it as before. You’ll need to define the array pointers in main and pass them...

  • Write a complete C program that inputs a paragraph of text and prints out each unique...

    Write a complete C program that inputs a paragraph of text and prints out each unique letter found in the text along with the number of times it occurred. A sample run of the program is given below. You should input your text from a data file specified on the command line. Your output should be formatted and presented exactly like the sample run (i.e. alphabetized with the exact spacings and output labels). The name of your data file along...

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

  • Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that...

    Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that will process monthly sales data for a small company. The data will be used to calculate total sales for each month in a year. The monthly sales totals will be needed for later processing, so it will be stored in an array. Basic Logic for main() Note: all of the functions mentioned in this logic are described below. Declare an array of 12 float/doubles...

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