MUCLecture 2023 12535120
MUCLecture 2023 12535120
MUCLecture 2023 12535120
1
Computer Skills & Programming I MSc. Baraa Hashim Kareem
and so on.
• Array names follow the same conventions as other variable names.
Declaring Arrays: To declare an array in C++, the programmer
specifies the type of the elements and the number of elements required
by an array as follows:
Syntax
type arrayName [ arraySize ];
• student scores:
• characters:
• names:
One-Dimensional Arrays
A one-dimensional array is a list of related variables. The general form of a one-
dimensional array declaration is:
2
Computer Skills & Programming I MSc. Baraa Hashim Kareem
Examples:
int sample[10];
float float_numbers[100];
char last_name[40];
int main()
{
int sample[10]; // reserves for 10 integers
int t;
// initialize the array
for(t=0; t<10; ++t) sample[t] = t*t;
// display the array
for(t=0; t<10; ++t)
cout << sample[t] << ' ';
return(0);
}
3
Computer Skills & Programming I MSc. Baraa Hashim Kareem
Example
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];
// Now outputs Opel instead of Volvo
#include <iostream>
int main()
{int b [] = {11, 45, 62, 70, 88};
cout << b[0] << endl;
//Outputs 11
cout << b[3] << endl;
}