Array homework help- please help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 1
Reputation: ladyhuslter is an unknown quantity at this point 
Solved Threads: 0
ladyhuslter ladyhuslter is offline Offline
Newbie Poster

Array homework help- please help

 
0
  #1
Oct 30th, 2008
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
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <stdlib.h>
  4. #include <fstream>
  5. using namespace std;
  6. void Loadit();
  7.  
  8.  
  9.  
  10. int main()
  11.  
  12. {
  13. ifstream inf;
  14. inf.open ("F:\c++\hmwk 8\arrays.txt");
  15. ifstream infile("F:\c++\hmwk 8\arrays.txt");
  16. if(!infile)
  17. {
  18. cerr <<"Input file could not be opened"<<endl;
  19.  
  20. exit(1);
  21. }
  22. ofstream outfile("F:\c++\hmwk 8\arraysoutput.txt");
  23. //Output file
  24.  
  25. if ( ! outfile )
  26. {
  27. cerr << "Output file could not be opened" << endl;
  28.  
  29. exit(1);// Error Trap - end program if input file does not open
  30. }
  31.  
  32.  
  33. {
  34.  
  35.  
  36. 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
  37.  
  38. //initialize elements of array n to 0
  39.  
  40.  
  41.  
  42. // 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
  43. // n[ i ] = 0;
  44. outfile << "Part I: Print out Contents of Array and Array Index Values" << endl << endl;
  45. outfile << "Element" << setw( 13 ) << "Value" << endl;
  46. // headers
  47. // output contents of array n in tabular format
  48. for ( int j = 0; j < 20; j++ )
  49. outfile << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl;
  50.  
  51. outfile <<endl<<endl<<endl;
  52.  
  53. outfile << "Part 2: Print out Contents of Array and Array Index Values" << endl;
  54. outfile << "From Locations 18 - 7 "<<endl <<endl;
  55. outfile << "Element" << setw( 13 ) << "Value" << endl;
  56. for ( int j = 18; j >6; j-- )
  57. outfile << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl;
  58. outfile<<endl<<endl<<endl;
  59.  
  60.  
  61.  
  62. outfile << "Part 3: Print out Contents of Array and Array Index Values" << endl;
  63. outfile << "For Even Number Index Values "<<endl <<endl;
  64. outfile << "Element" << setw( 13 ) << "Value" << endl;
  65. for ( int j = 0; j < 20; j+=2)
  66. outfile << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl;
  67. outfile << endl << endl << endl;
  68.  
  69. outfile << "Part 4: Print out Contents of Array and Array Index Values" << endl;
  70. outfile << "For Even Value Content and Index Values "<<endl <<endl;
  71. outfile << "Element" << setw( 13 ) << "Value" << endl;
  72. for ( int j = 0; j < 20; j+=2)
  73. if(n[j] % 2 ==0){
  74. outfile << setw( 7 ) << j << setw( 13 ) << n[ j-1 ] << endl;
  75. }
  76. outfile <<endl<<endl<<endl;
  77.  
  78. outfile << "Part 5: Print out the Largest Interger Stored in the Array" << endl<< endl;
  79.  
  80. index = j;
  81. int largest = n[20];
  82. for ( int j=0; j < n[0]; j++ ){
  83. if (n[j+1]> largest)
  84. n[j+1]=largest;
  85. for ( int j=0; j < n[0]; j++ )
  86. {
  87. int largest = n[20];
  88. for ( int j=0; j < n[0]; j++ ){
  89. if (n[j+1]> largest)
  90. n[j+1]=largest;
  91.  
  92.  
  93. cout << setw( 7 ) << j << setw( 13 ) << n[j] << endl;
  94. }
  95.  
  96.  
  97.  
  98.  
  99. outfile << "Part 6: Print out the Sum and Average of all Intergers Stored in the Array" << endl<< endl;
  100.  
  101. int totalARRAYvalues = 0;
  102. for(int j = 0; j < n[20]; j++)
  103. {
  104. totalARRAYvalues = totalARRAYvalues+ n[j];
  105. }
  106. outfile << "Total Array Value: " << endl;
  107. outfile << setw(21)<< totalARRAYvalues<<endl;
  108.  
  109. outfile << "Total Array Aveage: " << endl;
  110. outfile << setw(21)<< totalARRAYvalues<< endl;
  111.  
  112. outfile <<endl<<endl<<endl;
  113.  
  114. outfile << "Part 7: Print out the Location(s) and Content Values" << endl<< endl;
  115. outfile << setw(38)<<"Where the Content of the Array"<< endl<< endl;
  116. outfile << setw(34) <<"is 3 Times the Index Value" << endl<< endl;
  117.  
  118. for(int j = 1; j <= 20; j++)
  119.  
  120. if(n[j-1] * 3 == 20)
  121. {
  122. outfile << n[j-1] << " * 3 = " << j << "\n";
  123.  
  124. }
  125.  
  126. inStream.close();
  127. outfile.close();
  128. cout<<endl<<endl<<endl;
  129. system("PAUSE");
  130. return 0;
  131. }
Last edited by Ancient Dragon; Oct 30th, 2008 at 11:00 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,484
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Array homework help- please help

 
0
  #2
Oct 30th, 2008
You posted your assignment and code, so what now? Are we to just admire the code you posted?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Array homework help- please help

 
0
  #3
Oct 31st, 2008
Check your braces. There are problems with them.
I would love to change the world, but they won't give me the source code
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC