Below is my problem...
I think section 1-3 are okay, but I am having problems with sections 4-7,
Any help would be great.
I also need to load these from a file... I just loaded the array by initializing the values as I thought I could start there check my work to see if the defs are working and then write the functions and calls, and load the data from the file later... but I am really stuck.
Here's my code
#include <iostream>
#include <iomanip>
#include <stdlib.h> //for exit(1)
#include <fstream>
using namespace std;
//void Loadit();
/* ifstream inf;
inf.open ("C:CS201HWASSGN7INPUTFILE.txt", ios::in);
ifstream infile("C:CS201HWASSGN7INPUTFILE.txt", ios::in);
if(!infile)
{
cerr <<"Input file could not be opened"<<endl;
// Error Trap - end program if input file does not open
exit(1);
}
ofstream outfile("c:CS201HWASSGN7OUTPUTFILE.txt", ios::out);
//Output file
if ( ! outfile )
{
cerr << "Output file could not be opened" << endl;
//Error Trap - end program if output file does not open
exit(1);
}
*/
int main()
{
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;
cout << "Part I: Print out Contents of Array and Array Index Values" << endl << endl;
cout << "Element" << setw( 13 ) << "Value" << endl;
// headers
// output contents of array n in tabular format
for ( int j = 0; j < 20; j++ )
cout << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl;
cout<<endl<<endl<<endl;
cout << "Part 2: Print out Contents of Array and Array Index Values" << endl;
cout << "From Locations 18 - 7 "<<endl <<endl;
cout << "Element" << setw( 13 ) << "Value" << endl;
for ( int j = 18; j >6; j-- )
cout << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl;
cout<<endl<<endl<<endl;
cout << "Part 3: Print out Contents of Array and Array Index Values" << endl;
cout << "For Even Number Index Values "<<endl <<endl;
cout << "Element" << setw( 13 ) << "Value" << endl;
for ( int j = 0; j < 20; j+=2)
cout << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl;
cout<<endl<<endl<<endl;
cout << "Part 4: Print out Contents of Array and Array Index Values" << endl;
cout << "For Even Value Content and Index Values "<<endl <<endl;
cout << "Element" << setw( 13 ) << "Value" << endl;
for ( int j = 0; j < 20; j+=2)
if(n[j] % 2 ==0){
//if (n[j] % 2 !=0){
//j= (2+2 * j)
cout << setw( 7 ) << j << setw( 13 ) << n[ j-1 ] << endl;
}
// for ( int o = 0; o < 20; o++ )
// if(n[o]%2==0)
// {
cout<<endl<<endl<<endl;
cout << "Part 5: Print out the Largest Interger Stored in the Array" << endl<< endl;
int index = 0;
int largest;
largest = n[20];
for ( int j=0; j < 20; j++ )
{
// if (n[j]>largest)
if (largest !=j);
largest =n[j];
index = j;
cout<<"The Largest Interger is " << largest <<" @ location " << index<< 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;
cout << setw( 7 ) << j << setw( 13 ) << n[j] << endl;
}
*/ cout<<endl<<endl<<endl;
cout << "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];
}
cout << "Total Array Value: " << endl;
cout << setw(21)<< totalARRAYvalues<<endl;
cout << "Total Array Aveage: " << endl;
cout << setw(21)<< totalARRAYvalues<< endl;
cout<<endl<<endl<<endl;
cout << "Part 7: Print out the Location(s) and Content Values" << endl<< endl;
cout<< setw(38)<<"Where the Content of the Array"<< endl<< endl;
cout << setw(34) <<"is 3 Times the Index Value" << endl<< endl;
for(int j = 1; j <= 20; j++)
if(n[j-1] * 3 == 20)
{
cout << n[j-1] << " * 3 = " << j << "n";
}
// int largest = n[j];
/* int largest;
for ( int j = 0; j < 20; j++)
//int largest = n[j];
if(n[j+1] >largest)
{
n[j+1]=largest;
// cout << setw( 7 ) << j << setw( 13 ) << n[j] << endl;
cout << setw( 7 ) << j << setw( 13 ) << largest << endl;
}
*/
/* largestnum=num1;
if (num2>largestnum)
largestnum=num2;
if (num3>largestnum)
largestnum=num3;
*/
// cout<<endl<<endl<<endl;
system("PAUSE");
return 0;
}
Write a C++ program which will load 20 integers into
an array and then:
Print out the contents of the array as well as the
index values from locations 0 through 19.Print out the contents of the array and the index
values from locations 18 to 7.Print out the contents and index values for even
index values(i.e. locations 0, 2, 4, 6...).Print out the even valued contents and their index
values.Find and print-out the largest integer stored in
the array as well as its index location value.Find and print the sum and average of all integers
stored in the array.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