0% found this document useful (0 votes)
1 views

Array

The document provides various examples of C++ programs that demonstrate the use of arrays, including declaring, initializing, reading, and printing array elements. It covers tasks such as reading numbers, adding matrices, finding transposes, sorting numbers, searching in an ordered list, generating unique random numbers, and calculating student scores. Each program is structured with input and output instructions, showcasing fundamental programming concepts related to arrays.

Uploaded by

eng.ahmed.eg.94
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Array

The document provides various examples of C++ programs that demonstrate the use of arrays, including declaring, initializing, reading, and printing array elements. It covers tasks such as reading numbers, adding matrices, finding transposes, sorting numbers, searching in an ordered list, generating unique random numbers, and calculating student scores. Each program is structured with input and output instructions, showcasing fundamental programming concepts related to arrays.

Uploaded by

eng.ahmed.eg.94
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Arrays

Declaring arrays

Type array name [array_size];

Ex

X[0]
Int x [5] X[1]
X[2]
type name size
X[3]
X[4]

Initializing arrays
Type array_name[size] = [element1, element2, …..];

To read numbers to store in array


for (int I =0; I < 5; i++)
{
cin >> x[i];
}
To print elements of an array
for (int I = 0; I < 5; i++)
{
cout << x[i];
}

# write a program to read 30 numbers and print these


numbers in reverse order

# include <iostream.h>
void main (void)
{
Int a[30], I;
for (I = 0; I < 30; i++)
{
cout << “enter number”;
cin >> a[i];
}
cout << “the number in reverse order”;
for (I = 29; i >= 0; i--)
{
cout << a[i] << “\t”;
}
# write a program to add two matrices each matrix has
dimension 4 * 6

# include <iostream.h>
void main (void)
{
Int a[4][6], b[4][6], sum[4][6], r, c;
cout << “enter first matrix”;
for (r = 0; r <=3; r++)
{
for (c = 0; c <= 5; c++)
{
cout << ”a[“ << r << “] [“ << c << “] =”;
cin >> a[r][c];
}
cout << “enter second matrix”;
for (r = 0; r < 4; r++)
{
for (c = 0; c < 6; c++)
{
cout << “b[“ << r << “] [“ << c << “] =” ;
cin >> b[r][c];
}
cout << “the result \n”;
for (r = 0; r < 4; r++)
{
for (c = 0; c < 6; c++)
{
sum[r][c] = a[r][c] + b[r][c];
cout << sum[r][c] << “\t”;
}
}
cout << “\n”;
}
# write a program to read 3 * 5 matrix and print its
transpose (5 * 3)

# include <iostream.h>
void main (void)
{
Int m[3][5], r, c;
cout << “please enter 3 * 5 matrix”;
for (r = 0; r <=2; r++)
{ cout << “\n”;
} for (c = 0; c < 5; c++)
} {
cin >> m[r][c];
}
}
cout << “the transpose matrix”;
for (c = 0; c < 5; c++)
{
for ( r = 0; r < 3; r++)
{
cout << m[r][c] << “\t”;
}
# write a program to sort 10 number using selection sort
(descending order)

# include <iostream.h>
void main (void)
{
Int x[10], min, minloc, I, r, temp;
cout << “enter the 10 numbers;
for (I = 0; I < 10; i+)
{
cin >> x[i];
}
for (I = 0; I <= 8; i++)
{
min = x[i];
minloc = I;
}
for (r = I + 1; r <=9; r++)
{
if (x[r] < min)
{
min = x[r];
minloc = r;
}
temp = x[r];
x[r] = x[minloc];
x[minloc] = temp;
}
for (r = 0; r < 10; r++)
cout << x[r] << “\t”;
}
# write a program to search a number in an ordered list
using linear search
(8, 9, 10, -10, 5, 6, 7, 2, 1, 0)

# include <iostream.h>
void main (void)
{
Int y[] = {8, 9, 10, -10, 5, 6, 7, 2, 1, 0};
Int I, key;
cout << “enter the number”;
cin >> key;
for (I = 0; I < 10; i++)
{
if (y[i] == key)
{
cout << “the key does exist ” << I;
}
else
cout << “they key doesn’t exist”;
}
}
# write a program to generate 30 integer numbers with
values less than 20. The numbers must be unrepeated
so, any number must not be printed more than one time

# include <iostream.h>
void main (void)
{
Int y[30], I, m, f;
for (I = 0; I < 30; i++)
{
y[i] = rand() 20;
}
cout << “the numbers are “ << “\n” << y[0] << “\n”;
for (I = 1; I < 30; i++)
{
f = 0;
For (m = I – 1; m >=0; m--)
{
if (y[i] == y[m])
{
f++; break;
}
}
If (f == 0)
cout << y[i] << “\n”;
}
}
# write a program to read scores of 20 students in 5
subjects. Calculate the average score of each student,
and the success ratio of each subject

# include <iostream.h>
void main (void)
{
Int score[20][5], r, c;
Float sum[20] = {}, count[5] = {};
Cout << “enter the score of students in each subject”;
For(r = 0; r <20; r++)
{
for (c = 0; c < 5; c++)
{
cin >> score[r][c];
}
}
For (r = 0; r < 20; r++)
{
for (c = 0; c < 5; c++)
{
sum[r] = sum[r] + score[r][c];
}
}
For (r = 0; r < 20; r++)
{
cout << “average score for student”;
cout << r + 1 << “=” << sum[r] << “\n”;
}
for (c = 0; c < 5; c++)
{
for (r = 0; r < 20; r++)
{
if (score[r][c] > 50
count[c]++;
}
}
For (c = 0; c < 5; c++)
{
cout << “the success ratio for subject”;
Cout << c + 1 << “=” << (count[c] % 20)*100 << “\n”;
}
}
# write a program to print 10 unrepeated sorted
(ascending) random integer numbers with value less
than 20

# include <iostream.h>
void main (void)
{
Int y[10], I, m, min, minloc, r, k, temp, y[0] = rand() % 20;
For (I = 1; I < 10; i++)
{
y[i] = rand() % 20;
For (m = I; m > 0; m--)
{
if (y[i] == y[m])
{
m = I – 1;
}
}
}
for (y[r] < min)
{
min = y[r];
minloc = r;
}
}
For (r = 0; r < 10; r++)
{
cout << y[r];
}
}

You might also like