Arrays in C++
C++ data types fall into different categories. One of these categories is the structured data type. This chapter and the next few chapters focus on structured data types.
Recall that a data type is called simple if variables of that type can store only one value at a time. Ex: char, short, int, long, float and bool
In contrast, in a structured data type, each data item is a collection of other data items. Ex: array, struct, union, class. Simple data types are building blocks of structured data types.
Pointer data type used to store memory address of computer.
let us consider the following problem. We want to write a C++ program that reads five numbers, finds their sum, average, and prints the numbers in reverse order.
int num1, num2, num3, num4, num5;
int sum=0, ave=0;
cout<<"Enter number1"; cin>>num1;
cout<<"Enter number2"; cin>>num2;
cout<<"Enter number3"; cin>>num3;
cout<<"Enter number4"; cin>>num4;
cout<<"Enter number5"; cin>>num5;
sum =num1+num2+num3+num4+num5;
ave=sum/5;
cout<<"Sum ="<<sum<<"& Average ="<<ave;
cout<<"The number in reverse order ";
cout<<num5<<" "<<num4<<" "<<num3<<" "<<num2<<" "<<num1;
However, if you need to read and process 100 (or more) number, you would have to declare 100 variables and write many cin, cout statements. Thus, for large amounts of data, this type of program is not desirable.
So what?
- One Dimensional
- Multidimensional
Comments
Post a Comment