For this homework, you will read up to 1000 long integer values from a file into
ID: 3568201 • Letter: F
Question
For this homework, you will read up to 1000 long integer values from a file into an array, find the max, min and the ith value. Then you will sort the array (examples will be shown in class), and finally print the max, min and ith value for the sorted array.
Getting Started:
To get started, create a new project and copy-paste the code for Homework 9 (I have inserted this below) into a *.cpp file in the new project. Note that the code must be modified significantly for the new program.
As for the previous assignment, the code must prompt the user for an input filename and a value that is used as the position of a number in the array to display (It is the position in the array this time instead of in the file.). For example, if the user enters 100, then you will print the 100th number in the array as part of the output. If the user enters a number greater that the number of values in the array, then you print an error message (see below).
Requirements:
As with the last assignment, I specify in which functions certain code must be implemented; this is so that you learn how to use functions, arguments, parameters, arrays, etc. For example, you must declare the array of 1000 integers in main() and pass it as a parameter to both the readArray() and sortArray() functions, see below for the prototypes and descriptions for the functions. You actually use the same array throughout the program; you are sharing the memory space of the array declared in main() as though it were a reference parameter. You should declare a constant for the size of the array, 1000, and use it everywhere in the code where you would use 1000.
int main()
Explanation / Answer
for(i=0;i<s;i++){
for(j=i+1;j<s;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("After sorting is: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
return s
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.