Question
Use c++ as programming language. The file needs to be created ourselves

(ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional a
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

void readArray(int [], int);
void sortDescendingArray(int [], int);
void averageArray(int [], int);
void printReverseArray(int [], int);

FILE *inputFP, *outputFP;

int main(int argc, char const *argv[])
{
   int K;

   inputFP = fopen("input.txt", "r");
   outputFP = fopen("output.txt", "w+");

   fscanf(inputFP, "%d", &K);
  
   int array[K];

   readArray(array, K);

   sortDescendingArray(array, K);

   averageArray(array, K);

   printReverseArray(array, K);

   fclose(inputFP);
   fclose(outputFP);

   return 0;
}

void readArray(int array[], int N)
{
   fprintf(outputFP, "Taking input into the array.\n");

   for(int i=0; i<N; i++)
   {
       fscanf(inputFP, "%d", &array[i]);
   }

   return;
}

void printReverseArray(int array[], int N)
{
   fprintf(outputFP, "Writing the numbers in reverse order.\n");
  
   int i;

   for(i=0; i<N; i++)
   {
       if(i % 5 == 0)
           fprintf(outputFP, "\n");
       fprintf(outputFP, "%d ", array[i]);
   }

   return;
}

void sortDescendingArray(int array[], int N)
{
   fprintf(outputFP, "Sorting the array.\n");
   int i,j,k;
   int max;

   for(i=0; i<N-1; i++)
   {
       max = array[i];
       for(j=i; j<N; j++)
       {
           if(array[j] > max)
           {
               max = array[j];
               k = j;
           }
       }
       swap(array[i], array[k]);
   }
}

void averageArray(int array[], int N)
{
   int sum=0;
   for(int i=0; i<N; i++)
   {
       sum += array[i];
   }

   fprintf(outputFP, "The average of the array is %d.\n", sum/N);
}

Add a comment
Know the answer?
Add Answer to:
Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...
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
  • C programming Strictly - Write a program to sort an array of integers via arrays of...

    C programming Strictly - Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. Problem 1 (68 points): Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. The code should contain the following functions: 1)to randomly generate an array of integer numbers; 2) to allocate memory and build the arrays of pointers as shown in the figure...

  • Using C programming For this project, you have been tasked to read a text file with student grade...

    Using C programming For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

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

  • C- PROGRAMMING PROJECT #4 Design and Write a C program to calculate an average of an...

    C- PROGRAMMING PROJECT #4 Design and Write a C program to calculate an average of an array of numbers and produce the following output: 1. Your first and last name 2. Course Number 3. C Project Number 4. All the numbers in the array 5. The sum of all the numbers in the array 6. The count of all the numbers in the array 7. The average of all the numbers in the array Double-space after lines 3, 4, 5,...

  • C++ programming please Write a program that will display random numbers in order from low to...

    C++ programming please Write a program that will display random numbers in order from low to high. 1. Declare an integer array of size 100. Ask the user how many numbers to work with (n). It can be less than 100. 2. Generate random numbers from 10 to 50. 3. Write the random numbers to an output file named random.dat. Close the file. 4. Open random.dat for input and read the data values into your array. Pass your array to...

  • Language C Code Write a program that takes two integer arrays (A and B) and sums...

    Language C Code Write a program that takes two integer arrays (A and B) and sums them together (element wise). A third array to accept the result should be passed in as the output argument. Assume the arrays are all the same size. The argument N is the size of the arrays. Your code should provide a function with the following signature: void array Addition (int A[], int B[], int N, int output[]) { } Your code must also provide...

  • Arrays and reading from a file USE C++ to write a program that will read a...

    Arrays and reading from a file USE C++ to write a program that will read a file containing floating point numbers, store the numbers into an array, count how many numbers were in the file, then output the numbers in reverse order. The program should only use one array and that array should not be changed after the numbers have been read. You should assume that the file does not contain more than 100 floating point numbers. The file name...

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