Question

In: Computer Science

Overview: You will write a program that reads up to 100 numbers from a file. As...

Overview: You will write a program that reads up to 100 numbers from a file. As you read the
numbers, insert them into an array in ascending order.
Specifics:
1A. Write a function called insertIntoSortedArray .
i. It should take three arguments -
a. myArray[ ] : sorted array that should be able to hold at most 100 integers.
b. numEntries : the number of elements inserted so far.
c. newValue : the incoming value to be inserted into the sorted array (i.e.
myArray[]).
ii. The insertIntoSortedArray function should return a count of the elements inserted so
far (i.e. the current size of the array)
This function is defined as follows:
int insertIntoSortedArray ( int myArray[], int numEntries, int newValue);

Solutions

Expert Solution

//This program is written in C
//To convert it to C++, comment the first header line and
//uncomment next two lines as written below.

#include <stdio.h> //for C

//#include <iostream> // for C++
//using namespace std; // for C++

int insertIntoSortedArray (int myArray[], int numEntries, int newValue)
{
   //corner case: array already full
   if(numEntries == 100)
   {
       printf("Number can't be inserted, array full\n");
       return numEntries;
   }

   //start from the end of the array and shift all the elements
   //larger than newValue to one position right
   //when this loop stops, variable i will point to one left of the
   //position of newValue

   int i=numEntries-1;
   while(i>=0 && myArray[i]>newValue)
   {
       myArray[i+1]=myArray[i];
       i--;
   }
   myArray[i+1]=newValue;

   //new size of array
   numEntries++;
   return numEntries;

}


//test function to check the program
void printArray(int myArray[], int numEntries)
{
   for(int i=0; i<numEntries; i++)
       printf("%d ", myArray[i]);
   printf("\n");
}

int main()
{
   int myArray[100];
   int numEntries = 0;
   //open file
   FILE* fp = fopen("filename.txt", "r");
   int newValue;
   //read numbers from file
   while(!feof(fp))
   {
       fscanf(fp,"%d", &newValue);
       numEntries = insertIntoSortedArray(myArray,numEntries,newValue);
       printArray(myArray,numEntries);
       if(numEntries==100)
           break;
   }
   //close file
   fclose(fp);

   return 0;
}

Output for the following input:

5 4 6 10 7 1 6 9 6


Related Solutions

Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
4. Write a program that reads all numbers from a file and determines the highest and...
4. Write a program that reads all numbers from a file and determines the highest and lowest numbers. You must NOT use arrays to solve this problem! Write functions where appropriate. Programming language should be C
In this assignment you will write a PHP program that reads from a data file, creates...
In this assignment you will write a PHP program that reads from a data file, creates an associative array, sorts the data by key and displays the information in an HTML table. Create a PHP file called hw4.php that will do the following: - Display your name. - Read text data from a file. The data file is hw3.txt. The file hw3.txt will hold the following text: PQRParrot, Quagga, Raccoon DEFDo statements, Else statements, For statements GHIGeese, Hippos, If statements...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display...
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf()...
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf() function. Can you add few comments with explanations what is going on?
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a program that reads a file (provided as attachment to this assignment) and write the...
Write a program that reads a file (provided as attachment to this assignment) and write the file to a different file with line numbers inserted at the beginning of each line. Such as Example File Input: This is a test Example File Output 1. This is a test. (Please comment and document your code and take your time no rush).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT