C++ with Arrays

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

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

C++ with Arrays

 
-3
  #1
Oct 21st, 2009
// try out
//Program - 5.4
# include <iostream.h>
# include <conio.h>
void main ( )
{
int x[5] = {1,2,3,4,5}, y [5] = {5,4,3,2,1},
result [5] = { 0,0,0,0,0 };
int i= 0;
while (i++ < 5)
result [i] = x [i] - y [i];
clrscr ( );
cout << “\n The contents of the array are: \n”;
i= 0 ;
do
{
cout << ‘\t’ << x [i]
<< ‘\t’ << y [i]
<< ‘\t’ << result [i]<<‘\n’;
i++;
} while (i<5);
getch ( );
}


What is the output of the below program?
I want walk through of this program
  1. // try out
  2. //Program - 5.4
  3. # include <iostream.h>
  4. # include <conio.h>
  5. void main ( )
  6. {
  7. int x[5] = {1,2,3,4,5}, y [5] = {5,4,3,2,1},
  8. result [5] = { 0,0,0,0,0 };
  9. int i= 0;
  10. while (i++ < 5)
  11. result [i] = x [i] - y [i];
  12. clrscr ( );
  13. cout << “\n The contents of the array are: \n”;
  14. i= 0 ;
  15. do
  16. {
  17. cout << ‘\t’ << x [i]
  18. << ‘\t’ << y [i]
  19. << ‘\t’ << result [i]<<‘\n’;
  20. i++;
  21. } while (i<5);
  22. getch ( );
  23. }
  24.  
  25.  
  26. What is the output of the below program?
  27. I want walk through of this program
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 379
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz
 
0
  #2
Oct 21st, 2009
  1. int x[5] = {1,2,3,4,5}, y[5] = {5,4,3,2,1},
  2. result[5] = {0,0,0,0,0};

This creates 3 arrays of the type int, with the names x, y and result.

  1. int i = 0;
  2. while(i++ < 5)
  3. result[i] = x[i] - y[i];

This loops through a range of 1 to 5 with i as the current value, and puts x[i] - y[i] into result[i]

  1. clrscr();
this clears the console's text buffer(get's rid of all the text in the console screen)

  1. cout << “\n The contents of the array are: \n”;
  2. i= 0 ;
  3. do
  4. {
  5. cout << ‘\t’ << x [i]
  6. << ‘\t’ << y [i]
  7. << ‘\t’ << result [i]<<‘\n’;
  8. i++;
  9. } while (i<5);

'\n' = newline
'\t' a tab(usually equal to the width of 4 spaces)

this prints out all the contents of the arrays, one after eachother while i(which gets incremented(+1) each run through the loop) is smaller then 5.

  1. getch();
waits for the user to input something

If anyone see's something incorrect, or they think they can offer a better explanation, feel free to correct me.
...
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 379
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz
 
0
  #3
Oct 21st, 2009
Alternatively, you could visit this:

http://www.cplusplus.com/doc/tutorial/

And look at the things in there like loops, arrays, variables and stuff that you are curious about
Last edited by tomtetlaw; Oct 21st, 2009 at 7:12 am.
...
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC