/* MYPRINTOUT FOR ARRAY IS ALL WRONG WHICH IS THROWING EVERYTHING OFF the file h
ID: 668131 • Letter: #
Question
/*
MYPRINTOUT FOR ARRAY IS ALL WRONG WHICH IS THROWING EVERYTHING OFF the file has 98 numebrs but the array can hold up to 100 integers so i need to printout only the numebrs in the file then print how many NUMBERS were read NEED HELP THERE AN ERROR I CANT FIND OR MAYBE OVERTHINKING IT
Your program should handle up to 100 integer numbers. Pass the array as one of the parameters.
For each print out produced below, label the results. Also print your output in the order listed below.
read in the values into the array. Read to end of file. The input is found arraynum file on blackboard under assignments.
Print out all the values in the array with no more than 10 numbers per output line.
Print out how many numbers were read in.
Print out the sum of the numbers.
Print out the average of the numbers. (use the previous two functions?)
Print out how many numbers in the array are larger than the average and how many are smaller than the average. Pass average as a parameter.
For every odd location in the array (1,3,5,…), subtract it from the previous even location (0,2,4,…)
and store results in the odd location. Print out all values in the array (a previous requirement)
Print out the sum for the new array.
Print out the average for the new array.
Find the smallest value in the array and print out its value and its location in the array.
Convert every negative number to positive number. Print out the array.
Find the largest value in the array and print out its value and its location in the array.
Swap the first number in the array with the last number in the array. Print out the array.
Print out the average for the new array.
*/
HERES NUMBER IN FILE
24 -43 63 -72 73 72 7 -23 36 54
84 32 97 5 23 -7 62 32 3 7
45 78 28 93 33 33 64 -64 -83 -2
1 5 25 75 34 43 8 7 9 82
5 4 38 9 49 28 -15 32 -69 45
-24 4 8 55 39 72 32 27 8 83
22 27 64 7 44 26 45 66 83 62
52 -7 -36 62 78 -67 34 81 -3 2
27 55 11 -63 3 6 87 -4 -45 4
4 22 72 45 5 -11 2 66
#include<iostream>
#include<fstream>
using namespace std;
//function
void printarray(int array[],int size)
{
for(int i =0;i<size;i++)
{
cout<<array[i]<<" ";
if((i+1)%10==0)
cout<<endl;
}
}
//function
double average(int array[],int size)
{
double sum = 0.0;
for(int i=0;i<size;i++)
sum+=array[i];
return(sum/size);
}
//function
int amtNumRead(int array[],int size)
{
int count=0;
for(int i=0;i<size;i++)
{
array[i]=count;
count++;
}
return count;
}
//function
int sumOfNumbers(int array[],int size)
{
int sum =0;
for(int i =0;i<size;i++)
{
sum+=array[i];
}
return sum;
}
//funtion
void largest_than_average_small_then_average(int array[],double averageAmt,int&smallCount,int&largeCount,int size)
{
largeCount=0;
smallCount=0;
for(int i=0;i<size;i++)
{
if(array[i]>averageAmt)
{
largeCount++;
}else
{
smallCount++;
}
}
}
//functions
void print_largest_and_location(int array[],int size)
{
int largest=0;
int i;
int position;
for(i=0;i<size;i++)
{
if(largest>array[i])
{
largest= array[i];
position=i;
}
}
cout<<"The largest number in array is "<<array[i]<<" at position "<<position<<endl;
}
//functions
void print_smallest_and_location(int array[],int size)
{
int smallest=0;
int position;
int i;
for(i=0;i<size;i++)
{
if(smallest<array[i])
{
smallest= array[i];
position=i;
}
}
cout<<"The smallest number in array is "<<array[i]<<" at position "<<position<<endl;
}
//functions
void odd_even(int array[],int size)
{
for(int i=1; i<size; i+=2)
{
array[i-1] = array[i-1]-array[i];
}
}
//functions
void convert_negativeToPositive(int array[],int size)
{
for(int i=0; i<size; i++)
{
if(array[i]<0)
{
array[i]*=-1;
}
}
}
//function
void swap(int array[],int size)
{
int newArray;
for(int i =0;i<size;i++)
{
newArray=array[i];
array[i]=array[size-1];
array[size-1]=newArray;
}
}
int main()
{
const int size = 100;
int array[size];
int count=0;
double averageAmt;
int small;
int large;
ifstream infile;
infile.open("arraynum.txt");
//check for error
if (infile.fail())
{
cerr<<"Error opening file"<<endl;
exit(1);
}
while(!infile.eof())
{
for(int i=0;i<100&&infile;i++)
{
infile>>array[i];
}
}
infile.close();
// int numRead=amtNumRead(array,size);
printarray(array,size);
int numRead=amtNumRead(array,size);
int sum_num;
sum_num=sumOfNumbers(array,size);
averageAmt=average(array,size);
largest_than_average_small_then_average(array,averageAmt,small,large,size);
//coorect to numread
cout<<" The amount of numbers read was "<<numRead<<endl;
cout<<"The sum of integers from file is "<<sum_num<<endl;
cout<<"The average amount of the integers from file was "<<averageAmt<<endl;
cout<<"The numbers larger then average are "<<large<<endl;
cout <<"The numbers smaller than average are " << small << endl;
odd_even(array,size);
printarray(array,size);
cout<<endl;
sum_num=sumOfNumbers(array,size);
averageAmt=average(array,size);
cout<<"The sum of new array is "<<sum_num<<endl;
cout<<"The average of new array is "<<averageAmt<<endl;
print_smallest_and_location(array,size);
convert_negativeToPositive(array,size);
printarray(array,size);
print_largest_and_location(array,size);
swap(array,size);
printarray(array,size);
averageAmt=average(array,size);
cout<<"The average of new array is "<<averageAmt<<endl;
return 0;
}
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
//function
void printarray(int array[],int size)
{
for(int i =0;i<size;i++)
{
cout<<array[i]<<" ";
if((i+1)%10==0)
cout<<endl;
}
}
//function
double average(int array[],int size)
{
double sum = 0.0;
for(int i=0;i<size;i++)
sum+=array[i];
return(sum/size);
}
//function
int amtNumRead(int array[],int size)
{
int count=0;
for(int i=0;i<size;i++)
{
count++;
}
return count;
}
//function
int sumOfNumbers(int array[],int size)
{
int sum =0;
for(int i =0;i<size;i++)
{
sum+=array[i];
}
return sum;
}
//funtion
void largest_than_average_small_then_average(int array[],double averageAmt,int size)
{
int largeCount=0;
int smallCount=0;
for(int i=0;i<size;i++)
{
if(array[i]>averageAmt)
{
largeCount++;
}if(array[i]<averageAmt)
{
smallCount++;
}
}
cout<<"Number of numbers smaller than average: "<<smallCount<<" ";
cout<<"Number of numbers greater than average: "<<largeCount<<" ";
}
//functions
void print_largest_and_location(int array[],int size)
{
int largest=0;
int i;
int position;
for(i=0;i<size;i++)
{
if(largest>array[i])
{
largest= array[i];
position=i;
}
}
cout<<"The largest number in array is "<<array[i]<<" at position "<<position<<endl;
}
//functions
void print_smallest_and_location(int array[],int size)
{
int smallest=0;
int position;
int i;
for(i=0;i<size;i++)
{
if(smallest<array[i])
{
smallest= array[i];
position=i;
}
}
cout<<"The smallest number in array is "<<array[i]<<" at position "<<position<<endl;
}
//functions
void odd_even(int array[],int size)
{
for(int i=1; i<size; i+=2)
{
array[i-1] = array[i-1]-array[i];
}
printarray(array,size);
}
//functions
void convert_negativeToPositive(int array[],int size)
{
for(int i=0; i<size; i++)
{
if(array[i]<0)
{
array[i]*=-1;
}
}
}
//function
void swap(int array[],int size)
{
int newArray;
for(int i =0;i<size;i++)
{
newArray=array[i];
array[i]=array[size-1];
array[size-1]=newArray;
}
}
int main()
{
const int size = 100;
int array[size];
int count=0;
int c=0;
double averageAmt;
int small;
int large;
ifstream infile;
infile.open("arraynum.txt");
//check for error
if (infile.fail())
{
cerr<<"Error opening file"<<endl;
}
while(!infile.eof())
{
for(int i=0;infile&&i<100;i++)
{
c++;
infile>>array[i];
}
}
c=c-1;
infile.close();
// int numRead=amtNumRead(array,size);
printarray(array,c);
int numRead=amtNumRead(array,c);
cout<<" The amount of numbers read was "<<numRead<<endl;
int sum_num;
sum_num=sumOfNumbers(array,c);
printarray(array,c);
cout<<"The sum of integers from file is "<<sum_num<<endl;
averageAmt=average(array,c);
cout<<"The average amount of the integers from file was "<<averageAmt<<endl;
largest_than_average_small_then_average(array,averageAmt,c);
//coorect to numread
odd_even(array,c);
cout<<endl;
sum_num=sumOfNumbers(array,c);
averageAmt=average(array,c);
cout<<"The sum of new array is "<<sum_num<<endl;
cout<<"The average of new array is "<<averageAmt<<endl;
print_smallest_and_location(array,c);
convert_negativeToPositive(array,c);
printarray(array,c);
print_largest_and_location(array,c);
swap(array,c);
printarray(array,c);
averageAmt=average(array,size);
cout<<"The average of new array is "<<averageAmt<<endl;
return 0;
}
please comment
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.