THIS IS MY ASSIGNMENT


Write a C++ program which will load 20 integers into
an array and then:

1. Print out the contents of the array as well as the
index values from locations 0 through 19.

2. Print out the contents of the array and the index
values from locations 18 to 7.

3. Print out the contents and index values for even
index values(i.e. locations 0, 2, 4, 6...).

4. Print out the even valued contents and their index
values.

5. Find and print-out the largest integer stored in
the array as well as its index location value.

6. Find and print the sum and average of all integers
stored in the array.

7. Find and print-out location(s) and content
values, if any, where the content of the array
is three times the index value.

Use external input and output files with this program.
You must also use functions with this program. Create at least one function for each section as well as a loadIt() function.
Please label each output section and make it easy to
read. Use headings and columns. Format the output using the setw() manipulator.
Please minimize the use of global variables. Pass information to/from your functions via parameters or return values.
Data:
3 55 2 9 35
36 1 212 24 311
7 101 36 42 555 52
17 18 57 554


HERE IS MY PROGRAM

#include <iostream>
#include <iomanip>
#include <stdlib.h> 
#include <fstream>
using namespace std;
void Loadit();



int main()

{
 ifstream inf; 
inf.open ("F:\c++\hmwk 8\arrays.txt");
ifstream infile("F:\c++\hmwk 8\arrays.txt"); 
if(!infile)
{
cerr <<"Input file could not be opened"<<endl;

exit(1);
}
ofstream outfile("F:\c++\hmwk 8\arraysoutput.txt");
//Output file 

if ( ! outfile ) 
{
cerr << "Output file could not be opened" << endl;

exit(1);// Error Trap - end program if input file does not open
}


{


int n[20] = {3, 55, 2, 9, 35, 36, 1, 212, 24, 311, 7, 101, 36, 42, 555, 52, 17, 18, 57, 554}; // r is an array of 20 integers

//initialize elements of array n to 0



// for ( int i = 0; i < 20; i++ )//initializing the elements of array n to 0 as long as the elements are less than 20 add 1 
// n[ i ] = 0; 
outfile << "Part I: Print out Contents of Array and Array Index Values" << endl << endl;
outfile << "Element" << setw( 13 ) << "Value" << endl;
// headers
// output contents of array n in tabular format
for ( int j = 0; j < 20; j++ ) 
outfile << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl;

outfile <<endl<<endl<<endl;

outfile << "Part 2: Print out Contents of Array and Array Index Values" << endl;
outfile << "From Locations 18 - 7 "<<endl <<endl;
outfile << "Element" << setw( 13 ) << "Value" << endl;
for ( int j = 18; j >6; j-- ) 
outfile << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl;
outfile<<endl<<endl<<endl;



outfile << "Part 3: Print out Contents of Array and Array Index Values" << endl;
outfile << "For Even Number Index Values "<<endl <<endl;
outfile << "Element" << setw( 13 ) << "Value" << endl;
for ( int j = 0; j < 20; j+=2) 
outfile << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl;
outfile << endl << endl << endl; 

outfile << "Part 4: Print out Contents of Array and Array Index Values" << endl;
outfile << "For Even Value Content and Index Values "<<endl <<endl;
outfile << "Element" << setw( 13 ) << "Value" << endl;
for ( int j = 0; j < 20; j+=2) 
if(n[j] % 2 ==0){
outfile << setw( 7 ) << j << setw( 13 ) << n[ j-1 ] << endl;
}
outfile <<endl<<endl<<endl; 

outfile << "Part 5: Print out the Largest Interger Stored in the Array" << endl<< endl;

 index = j;
int largest = n[20];
for ( int j=0; j < n[0]; j++ ){
if (n[j+1]> largest)
n[j+1]=largest;
for ( int j=0; j < n[0]; j++ ) 
{ 
int largest = n[20];
for ( int j=0; j < n[0]; j++ ){
if (n[j+1]> largest)
n[j+1]=largest;


cout << setw( 7 ) << j << setw( 13 ) << n[j] << endl;
}




outfile << "Part 6: Print out the Sum and Average of all Intergers Stored in the Array" << endl<< endl;

int totalARRAYvalues = 0;
for(int j = 0; j < n[20]; j++)
{
totalARRAYvalues = totalARRAYvalues+ n[j];
}
outfile << "Total Array Value: " << endl;
outfile << setw(21)<< totalARRAYvalues<<endl;

outfile << "Total Array Aveage: " << endl;
outfile << setw(21)<< totalARRAYvalues<< endl;

outfile <<endl<<endl<<endl; 

outfile << "Part 7: Print out the Location(s) and Content Values" << endl<< endl;
outfile << setw(38)<<"Where the Content of the Array"<< endl<< endl;
outfile << setw(34) <<"is 3 Times the Index Value" << endl<< endl;

for(int j = 1; j <= 20; j++)

if(n[j-1] * 3 == 20)
{
outfile << n[j-1] << " * 3 = " << j << "\n";

}

inStream.close();
outfile.close();
cout<<endl<<endl<<endl;
system("PAUSE");
return 0; 
}
This article has been dead for over six months. Start a new discussion instead.