C++ 1. Place the following values in a file. Use this file as input to your prog
ID: 3766475 • Letter: C
Question
C++
1. Place the following values in a file. Use this file as input to your program. {62, 8, 25, 7, 90, 82, 22, 46, 15, 54}
2. Write a program that performs the following:("For" loops only except C)
a. Read 10 integers from the input file into a 10 position array.
b. Output the 10 integers on the same line from the array.
c. Output the 4th integer in the array.
d. Compute and output the sum and average of the last 6 integers in the array.
e. Find and output the smallest integer in the array.
f. find and output the largest integer in the array.
g. Output the firs 4 intergers in the array on the same line in reverse order.
h. Sort the integers in the array.(you may want to use the function selsort)
i. output the 10 integers on the same line from the array to confirm that the sort took place correctly.
3. All 10 integers must be read into the arry before any other processing can be accomplished. That is steps 1b - 1i cannot be performed during the reading of the integers. The purporse of this exercise is to practice manipulating a single dimension array.
4. Write all output to a file.
Explanation / Answer
1,
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
ifstream f;
f.open("data.txt");
int a;
while(f)
{
f>>a;
cout<<a;
}
getch();
}
2.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
ifstream f;
f.open("data.txt");
int i=0,a[10],sum=0;
while(f)
{
f>>a[i];
cout<<a[i]<<",";
i++;
if(i>3)
sum=sum+a[i];
}
cout<<a[3];
cout<<"sum of last 6 number is "<<sum;
av=sum/6;
cout<<"Average is "<<av;
int max=0;
for(i=0;i<10;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
cout<<"Maximum is "<<max;
int max=a[0];
for(i=0;i<10;i++)
{
if(a[i<min)
{
min=a[i];
}
}
cout<<"Minimum is "<<min;
getch();
}
3.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
ifstream f;
f.open("data.txt");
int i=0,a[10],sum=0;
while(f)
{
f>>a[i];
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.