944,087 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 316
  • C++ RSS
Oct 21st, 2009
-3

C++ with Arrays

Expand Post »
// 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
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
srmcsclab is offline Offline
1 posts
since Oct 2009
Oct 21st, 2009
0
Re: C++ with Arrays
c++ Syntax (Toggle Plain Text)
  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.

c++ Syntax (Toggle Plain Text)
  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]

c++ Syntax (Toggle Plain Text)
  1. clrscr();
this clears the console's text buffer(get's rid of all the text in the console screen)

c++ Syntax (Toggle Plain Text)
  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.

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 9
Solved Threads: 5
Posting Pro
tomtetlaw is offline Offline
591 posts
since Sep 2008
Oct 21st, 2009
0
Re: C++ with Arrays
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.
Reputation Points: 9
Solved Threads: 5
Posting Pro
tomtetlaw is offline Offline
591 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Insert Problem in Using a MySQL Database with C++
Next Thread in C++ Forum Timeline: Ms-Access as back end for files in cpp





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC