Question

Instructions: You will use two files for this lab: your C program file, and a separate text file containing the integer dataFunction #1. Define a function named computeAvg to compute the average of the values in an array. This function has two param

#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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

We have to store the input file in bin folder C:\Program Files (x86)\Dev-Cpp MinGW64\bin As I installed Dev C++ in C Drive

// myfile2.txt (input file)

34
45
56
67
78
89
91
21
32


=================================

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>

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

// Define computeAvg here
double computeAvg(int arr[],int count);
// Define findMax here
int findMax(int arr[],int count);

// Define findMin here
int findMin(int arr[],int count);

// Define selectionSort here ( copy from zyBooks 11.6.1 )
void selectionSort(int arr[],int count);

// Define binarySearch here
int binarySearch(int arr[],int count,int key);

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. */
avgOfArray=computeAvg(arrOfValues,counter);
maxValue=findMax(arrOfValues,counter);
minValue=findMin(arrOfValues,counter);
selectionSort(arrOfValues,counter);
printf("Enter a number to search :");
scanf("%d",&singleNum);
int index=binarySearch(arrOfValues,counter,singleNum);
printf("Average of array :%.2f\n",avgOfArray);;
printf("Maximum value in the array :%d\n",maxValue);
printf("Minimum value in the array :%d\n",minValue);
if(index==-1)
{
    printf("The number %d not found.\n");
}
else
{
       printf("The number %d is found at index %d\n",singleNum,index);
}


return 0;
}

// This function will compute the average of array
double computeAvg(int arr[],int count)
{
   int i;
   double sum=0;
   for(i=0;i<count;i++)
   {
       sum+=arr[i];
   }
   return sum/count;
}

// This function will find the maximum value in the array
int findMax(int arr[],int count)
{
   int i;
   int max=arr[0];
   for(i=0;i<count;i++)
   {
       if(max<arr[i])
       max=arr[i];
   }
   return max;  
}

// This function will find the minimum value in the array
int findMin(int arr[],int count)
{
   int i;
   int min=arr[0];
   for(i=0;i<count;i++)
   {
       if(min>arr[i])
       min=arr[i];
   }
   return min;  
}

// This function will sort the array
void selectionSort(int arr[],int count)
{
   int minIndex,i,j;
  
for (i = 0; i < count - 1; i++) {
// find smallest element in list starting at location i
minIndex = i;
for (j = i + 1; j < count; j++)
if (arr[j] < arr[minIndex])
minIndex = j;
// swap list[i] with smallest element
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}

}

// This function will search for value using binary Search Algorithm
int binarySearch(int arr[],int count,int target)
{ int size =count;
int first = 0, last = size - 1, middle, position = -1;
bool found = false;
while (!found && first <= last) {
middle = (first + last) / 2;
if (arr[middle] == target) {
position = middle;
return position;
} else if (arr[middle] > target)
last = middle - 1;
else
first = middle + 1;
}
return position;
}

=============================================

Output:

C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\ReadFileMinMaxAverageSelection SortBinarySearch.exe Opening file myfile2.txt. Read

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
#include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort,...
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
  • 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...

  • 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....

  • sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size,...

    sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size, c;     float median;     printf("Enter the array size:\n");     scanf("%d", &size);     array = (int*) malloc(size * sizeof(int));     printf("Enter %d integers:\n", size);     for (c = 0; c < size; c++)         scanf("%d", &array[c]);     sort(array, size);     printf("Array sorted in ascending order:\n");     for (c = 0; c < size; c++)         printf("%d ", array[c]);     printf("\n");     median = find_median(array,...

  • Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing...

    Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing an array of structs to a function, and returning a struct as the value of a function. A function in the program should read several rows of data from a text file. (The data file should accompany this assignment.) Each row of the file contains a month name, a high temperature, and a low temperature. The main program should call another function which...

  • 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:...

  • 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments

    9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in the main program that reads an integer N  (that is not more than 50) from standard input and then reads N  integers from a file named...

  • Task 4: a) Write a function named change() that has an integer parameter and six integer...

    Task 4: a) Write a function named change() that has an integer parameter and six integer reference parameters named hundreds, fifties, twenties, tens, fives, and ones. The function is to consider the passed integer value as a dollar amount and convert the value into the fewest number of equivalent bills. Using the reference parameters, the function should alter the arguments in the calling function. b) Include the function written in part a in a working program. Make sure your function...

  • Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std;...

    Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std; int read_function(int array[], int, int); int main() { int array[300], numba; read_function(array[300]); cout << "Enter a whole number between 2-20: " << endl; cin >> numba; read_function(numba); return 0; } int read_funtion (int arr[300], int num, int Values) { int sum=0; ifstream array_file; array_file.open("Lab8.dat"); for(int i=0; i < 300; i++) {   array_file >> arr[i];   cout << arr[i];   sum += i; } cout << sum;...

  • You are to write three functions for this lab: mean, remove, display. Download the main.cpp file...

    You are to write three functions for this lab: mean, remove, display. Download the main.cpp file provided to get started. Read the comments in the main function to determine exactly what the main function should do. //include any standard libraries needed // - Passes in an array along with the size of the array. // - Returns the mean of all values stored in the array. double mean(const double array[], int arraySize); // - Passes in an array, the size...

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