English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

#include

#define MAX 20

main()
{

int number[MAX] = {12,33,45,66,43,1,56,78,101,99};
int index;

cout.setf(ios::right);
for (index = 0; index < MAX; index++)
{
cout << endl
<< "THE CONTENTS OF ARRAY ELEMENT";
cout.width(4);
cout << (index + 1) << " IS :";
cout.width(4);
cout << number[index];
}
cout << endl << endl;
return(0);
}

2006-11-04 05:16:29 · 2 answers · asked by Best Helper 4 in Computers & Internet Programming & Design

2 answers

It's pretty clear that the code prints out the contents of an array, using a loop to generate indices ranging from 0 to MAX-1. The indices are used to index the array and print each element in turn.

Go through the code carefully, and you should be able to follow it. When you find something that puzzles you, such as how arrays work, read the appropriate section of the textbook.

2006-11-04 05:20:45 · answer #1 · answered by arbeit 4 · 1 0

Ok, here we start:

// A preprocessor statement to include a header, deprecated *.h
// New format uses the '#include '.
#include
// New format would also use 'using namespace std;' here.
// Preprocessor statement to define a literal value.
#define MAX 20

// main() begins at opening brace.
main()
{

// Allocates an integer array named number with MAX(20)
// elements and initializes them here.
int number[MAX] = {12,33,45,66,43,1,56,78,101,99... // Assumes
// missing closing brace and more numbers in array above.

// Allocates another integer called index, all allocations are made
// on the stack with scope and lifetime local to main().
int index;

// This call to the cout object's setf() function sets the right
// justification flag.
cout.setf(ios::right);

// The initialization statements needed by a for loop, using MAX as
// the limit to the index value.
for (index = 0; index < MAX; index++)
{// The for loop's statement group begins at this opening brace.
// Outputs a newline to the screen and the title.
cout << endl
<< "THE CONTENTS OF ARRAY ELEMENT";

// Cout object's width() function sets width at 4
cout.width(4);
// Outputs corrected count number for pass through the loop and
// the word 'IS' with appropriate space padding and punctuation.
cout << (index + 1) << " IS :";
// Cout object's width() function sets width at 4
cout.width(4);
// Outputs the current array member referred to by index.
cout << number[index];
}// Loop returns to top here.
// Outputs two newlines to the screen.
cout << endl << endl;
// Since all functions in C++ are assumed to return an integer if
// no return is specified, this returns an integer to satisfy the case.
return(0);
}// Program termination here.

I hope the above line by line explanation will help you understand enough to do the next ones by yourself.

2006-11-04 05:49:07 · answer #2 · answered by Anonymous · 0 1

fedest.com, questions and answers