0% found this document useful (0 votes)
4 views21 pages

برمجة

The document provides a comprehensive overview of arrays in C++, including their definition, importance, and syntax for declaration and initialization. It includes various tasks and examples related to array operations, such as finding sums, inserting and removing elements, and handling user input. Additionally, it covers specific programming challenges that involve manipulating arrays, demonstrating their practical applications in coding.

Uploaded by

mitodexiii
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)
4 views21 pages

برمجة

The document provides a comprehensive overview of arrays in C++, including their definition, importance, and syntax for declaration and initialization. It includes various tasks and examples related to array operations, such as finding sums, inserting and removing elements, and handling user input. Additionally, it covers specific programming challenges that involve manipulating arrays, demonstrating their practical applications in coding.

Uploaded by

mitodexiii
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/ 21

Task 1: Introduction to Arrays

Arrays are one of the fundamental concepts in the C++ programming language. An
array is a collection of elements, all of the same data type, stored in contiguous
memory locations. Arrays allow programmers to store multiple values under a
single variable name, which can be accessed using an index.

Importance of Arrays in C++:


- Efficient data management: Instead of declaring multiple variables, an array can
hold large amounts of data with just a single declaration.
- Code readability: Code becomes easier to read and maintain when arrays are used.
- Performance: Accessing elements using indexes is fast and efficient due to the
contiguous memory allocation.
- Support for loops: Arrays work well with loops (such as for or while) to
manipulate multiple elements systematically.

Role of Arrays in Programming:


Arrays are used in various applications, including:
- Storing records and datasets.
- Performing operations on a series of values (e.g., summing, sorting, searching).
- Representing matrices, lists, and other data structures.

By using arrays, programmers can manage data efficiently and write cleaner, more
organized code.

Task 2: Defining an Array and Declarations


Syntax of Array Declaration in C++:
To declare an array in C++, you specify:
data_type array_name[array_size];
- data_type: The type of elements (e.g., int, float, char).
- array_name: The identifier used to access the array.
- array_size: The number of elements the array will hold.

Example: Declaration and Initialization:


#include <iostream>
using namespace std;

int main() {
int numbers[5]; // Declaration of an array of 5 integers
// Initialization using loop
for (int i = 0; i < 5; i++) {
numbers[i] = i * 2; // Assign even numbers
}

// Displaying the array elements


for (int i = 0; i < 5; i++) {
cout << "Element at index " << i << ": " << numbers[i] << endl;
}

return 0;
}

Explanation:
- The array numbers is declared to store 5 integers.
- A for loop initializes the array with even numbers: 0, 2, 4, 6, 8.

1.what the output of the code:

Output: 5 -1 8 3 -1

Int hold [5];


For (int i = 0; i < 5; i++)
Hold [i] =2 *i-3;
For (int i =0;i < 5;i ++)
Cout<< hold [i] << " ";
Cout<< endl;

Hold [0] =hold [4];


Hold [4] =hold [1];
Hold [2] =hold [3] + hold [0];
For (int i = 0; i < 5; i++)
Cout<< hold[i] << " ";
Cout<<endl;
2.suppose list is an array of five elements of type int. what is stored in list after the
following c++ code executes?

Output: 2 7 6 11 1o

For (int i = 0; i < 5; i++){

List[i] =2 *i +5;

If(i % 2==0)

List[i] = List[i] -3;}

3. Suppose list is an array of six components of type int. What is stored in list after the
following C++ code executes?

Output: 5 6 9 19 23 37

list[0] = 5;

for (int i = 1; i < 6; i++) {

list[i] = i * i + 5;

if (i > 2)

list[i] = 2 * list[i] - list[i - 1]; }


4. Correct the following code so that it correctly initializes and outputs the elements of the
array myList; int

myList[10];

for (int i = 1; i <= 10; i++)

cin >> myList;

for (int i = 1; i <= 10; i++)

cout << myList[i] << " ";

cout << endl;

5. What is the output of the following code?

Output:

0 -2

1 6

2 -12

3 1

4 13

int list[] ={6, 8, 2, 14, 13};

for (int i = 0; i < 4; i++)

list[i] = list[i] - list[i + 1];

for (int i = 0; i < 5; i++)

cout << i << " " << list[i] << endl;


6. Suppose that you have the following function definition. void sum(int x, int y, int&
z) { z = x + y; } and

Consider the following declarations: int list1[10], list2[10], list3[10]; int a, b, c;

A/sum(a, b, c); or sum(list1[0], list2[0], a);

7. What is the output of the following program?

Output:

List elements: 11 16 21 26 30

#include <iostream>

using namespace std;

int main()

int count;

int alpha[5];

alpha[0] = 5;

for (count = 1; count < 5; count++)

alpha[count] = 5 * count + 10;

alpha[count - 1] = alpha[count] - 4;

cout << "List elements: ";

for (count = 0; count < 5; count++)

cout << alpha[count] << " ";


cout << endl;

return 0; }
8. What is the output of the following program?

output:

One contains: 3 8 13 18 23

#include <iostream.h>

int main()

int j;

int one[5];

int two[10];

for (j = 0; j < 5; j++)

one[j] = 5 * j + 3;

cout << "One contains: ";

for (j = 0; j < 5; j++)

cout << one[j] << " ";

cout << endl;

for (j = 0; j < 5; j++)

two[j] = 2 * one[j] - 1;

two[j + 5] = one[4 - j] + two [j];

cout << "Two contains: ";

for (j = 0; j < 10; j++)

cout << two[j] << " ";

cout << endl;

return 0; }
9. A car dealer has 10 salespersons. Each salesperson keeps track of the number of cars sold
each month and

reports it to the management at the end of the month. Write the code to store the number of
cars sold by each

salesperson in the array cars, output the total numbers of cars sold at the end of each
month, and output the

salesperson number selling the maximum number cars.

int mumber[10], sum=0,high;

for(int i=0;i<=9;i++){

cout<<"Enter how many car you sale:";

cin>>mumber[i];}

for(int i=0;i<=9;i++){

sum +=mumber[i];}

cout<<"Total sales of cars:"<<sum<<endl;

high =mumber[0];

for(int i=0;i<=9;i++){

if(high<mumber[i])

high=i;}

cout<<"higher mumber sales cars is mumber:"<<high;

10.Write function to initialize an int array to 0.

Void init(int A[],int size){


For(int i=0;i<size;i++){
A[i]=0;
}
}
11. Write a program that asks the user to type 10 integers of an array and an integer
value V. The program must

search if the value V exists in the array and must remove the first occurrence of V,
shifting each following

element left and adding a zero at the end of the array. The program must then write
the final array.

int A[10], V, shift=-1;

for(int i=0;i<=9;i++){

cout<<"Enter element of index "<<i<<":";

cin>>A[i];

cout<<"Enter the Element V you want to remove:";

cin>>V;

for(int i=0;i<=9;i++){

if(A[i]==V){

shift=i;

break;

if(shift != -1){

for(int i=shift;i<=9-1;i++){

A[i]=A[i+1];

A[9]=0;

for(int i=0;i<=9;i++){

cout<<A[i]<<" "; }
12. Write a program that asks the user to type 10 integers of an array and an integer value V
and an index value i between 0 and 9. The program must put the value V at the place i in the
array, shifting each element right and dropping off the last element. The program must then
write the final array.

const int SIZE = 10;

int arr[SIZE];

int V, i;

cout << "Enter 10 integers:" << endl;

for (int j = 0; j < SIZE; j++) {

cin >> arr[j];

cout << "Enter the value to insert (V): ";

cin >> V;

cout << "Enter the index (0 to 9) where to insert V: ";

cin >> i;

if (i < 0 || i >= SIZE) {

cout << "Invalid index. Must be between 0 and 9." << endl;

return 1;

for (int j = SIZE - 1; j > i; j--) {

arr[j] = arr[j - 1];

arr[i] = V;

cout << "Final array: ";

for (int j = 0; j < SIZE; j++) {

cout << arr[j] << " "; }


13. Write a program which takes 2 arrays of 10 integers each, a and b. c is an array
with 20 integers. The

program should put into c the appending of b to a, the first 10 integers of c from
array a, the latter 10 from b.

Then the program should display c.

int SIZE = 10;

int a[SIZE], b[SIZE], c[2 * SIZE];

cout << "Enter 10 integers for array a:" << endl;

for (int i = 0; i < SIZE; i++) {

cin >> a[i];

cout << "Enter 10 integers for array b:" << endl;

for (int i = 0; i < SIZE; i++) {

cin >> b[i];}

for (int i = 0; i < SIZE; i++) {

c[i] = a[i]; // First 10 from a

c[i + SIZE] = b[i]; // Next 10 from b}

cout << "Array c (combined): ";

for (int i = 0; i < 2 * SIZE; i++) {

cout << c[i] << " ";}


14. Write a function to find and return the sum of the elements of an int array.

void sum(int A[],int size){

int summation=0

for(int i=0;i<size;i++){

summation +=A[i];

Cout<<"The summation is="<<summation;

15. write function to find and return the index of the first largest element in an int
array.

void largest(int A[],int size){

int larg=A[0];

for(int i=0;i<size;i++){

if(larg <A[i]){

large=I;

Cout<<"The index of largest element is="<<larg

16. Write a function to copy some or all of the elements of one array into another
array.

void copyarr(int A[],int B[],int size){

for(int i=0;i<size;i++){

B[i]=A[i];

}
17. suppose two array the first one for store students’ course grades, together with
their ID numbers(are 5 digits

long) for maximize 50 students in a class. Write program to read and print the data
of n

int n, ID[50], GRADES[50];


cout<<"Enter the number of students(max 50):";
cin>>n;
for(int i=0;i<n;i++){
cout<<"Enter 5digits-ID:";
cin>>ID[i];
cout<<"Enter grades of student:";
cin>>GRADES[i];
}
cout << "\nStudent Data:\n";
cout << "ID\tGrade\n";
for (int i = 0; i < n; i++) {
cout << ID[i] << "\t" << GRADES[i] << endl;
}

18. Write a C++ program that declares an array alpha of 50 components of type
double, the first 25 components

are equal to the square of the index variable, and the last 25 components are equal
to three times the index

variable. Output the array so that 10 elements per line are printed.

double alpha[50];

for(int i=0;i<50;i++){
if(i<26)

alpha[i]=i*i;

else

alpha[i]=i+i+i;

for(int i=0;i<50;i++){

cout<<alpha[i]<<" ";

if(i % 10 == 0)

cout<<endl;

19. Write a function, smallestIndex, that takes as parameters an int array and its size
and returns the index of

the first occurrence of the smallest element in the array

void smallest(int A[],int size){

int small=A[0];

for(int i=0;i<size;i++){

if(small >A[i]){

small=I;

Cout<<"The index of smallest element is="<<small;


20. Write a function, lastLargestIndex, that takes as parameters an int array and its
size and returns the index of

the last occurrence of the largest element in the array

int lastLargestIndex(int arr[], int size) {

int maxVal = arr[0];

int lastIndex = 0;

for (int i = 1; i < size; i++) {

if (arr[i] > maxVal) {

maxVal = arr[i];

lastIndex = i;

} else if (arr[i] == maxVal) {

lastIndex = i;

21. Write a program that prompts the user to input 10 characters and outputs the
characters in uppercase letters.

(Use a character array to store them)

char chars[10];
cout << "Enter 10 characters:" << endl;
for (i = 0; i < 10; i++) {
cin >> chars[i];
}
for (i = 0; i < 10; i++) {
if (chars[i] >= 'a' && chars[i] <= 'z') {
chars[i] = chars[i] - ('a' - 'A'); // or chars[i] -= 32;
}
}
cout << "Characters in uppercase: ";
for (i = 0; i < 10; i++) {
cout << chars[i] << " ";
}
22. Write a program to find the frequency a specific number into int array.

int freq=0, A[10], num;

cout<<"Enter the element of array:";

for(int i=0;i<10;i++){

cin>>A[i];

cout<<"enter the number that you want find freq:";

cin>>num;

for(int i=0;i<10;i++){

if(num==A[i]){

freq++;

cout<<"the number of frequncy:"<<freq;

23. Write a program to find average of n element into int array except minimum
number.

int n, i, min, sum = 0, minIndex = -1;


cout << "Enter number of elements: ";
cin >> n;
int arr[100];
cout << "Enter " << n << " integers:" << endl;
for (i = 0; i < n; i++) {
cin >> arr[i];
}
min = arr[0];
minIndex = 0;
for (i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
minIndex = i;
}
}
for (i = 0; i < n; i++) {
if (i != minIndex) {
sum += arr[i];
}
}
double average = (double)sum / (n - 1);
cout << "Average excluding the minimum " << min << " is: " << average << endl;

24. Write a program to find position of specific element into int array.

int arr[100], n, i, target, position = -1;

cout << "Enter number of elements: ";

cin >> n;

cout << "Enter " << n << " integers:" << endl;

for (i = 0; i < n; i++) {

cin >> arr[i];

cout << "Enter the element to find: ";

cin >> target;

// Step 4: Search for the target

for (i = 0; i < n; i++) {

if (arr[i] == target) {

position = i;

if (position != -1)

cout << "Element found at index: " << position << endl;

else

cout << "Element not found in the array." << endl;


25. Write a program to find value of Z (where x and y are one dimension array):

int n, x[100], y[100], z = 0;

cin >> n;

for (int i = 0; i < n; i++) cin >> x[i];

for (int i = 0; i < n; i++) cin >> y[i];

for (int i = 0; i < n; i++)

z += x[i] * y[i];

cout << "Z = " << z << endl;

26. Write a program to find product array of each element in array int x and y.

int n, x[100], y[100], product[100];

cin >> n;

for (int i = 0; i < n; i++) cin >> x[i];

for (int i = 0; i < n; i++) cin >> y[i];

for (int i = 0; i < n; i++) {

product[i] = x[i] * y[i];

cout << "Product[" << i << "] = " << product[i] << endl;
27. Write a program to generate two arrays, the first one for store the odd elements
in one dimension array and

the second for store even elements for this array.

int arr[100], odd[100], even[100];

int n, o = 0, e = 0;

cin >> n;

for (int i = 0; i < n; i++) cin >> arr[i];

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 0)

even[e++] = arr[i];

else

odd[o++] = arr[i];

cout << "Odd array: ";

for (int i = 0; i < o; i++) cout << odd[i] << " ";

cout << "\nEven array: ";

for (int i = 0; i < e; i++) cout << even[i] << " ";

cout << endl;


28. Write a program to swap the minim element with the largest element in int
array.

int arr[100], n, i;

int minIndex = 0, maxIndex = 0;

cout << "Enter number of elements: ";

cin >> n;

cout << "Enter " << n << " integers:" << endl;

for (i = 0; i < n; i++) {

cin >> arr[i]; }

for (i = 1; i < n; i++) {

if (arr[i] < arr[minIndex]) {

minIndex = i;

if (arr[i] > arr[maxIndex]) {

maxIndex = i;

int temp = arr[minIndex];

arr[minIndex] = arr[maxIndex];

arr[maxIndex] = temp;

cout << "Array after swapping min and max elements:" << endl;

for (i = 0; i < n; i++) {

cout << arr[i] << " ";

}
29. Write program find the position of min and max element in one dimension array.

int arr[100], n, i;

int minIndex = 0, maxIndex = 0;

cout << "Enter number of elements: ";

cin >> n;

cout << "Enter " << n << " integers:" << endl;

for (i = 0; i < n; i++) {

cin >> arr[i];

for (i = 1; i < n; i++) {

if (arr[i] < arr[minIndex]) {

minIndex = i;

if (arr[i] > arr[maxIndex]) {

maxIndex = i;

cout << "Minimum element is " << arr[minIndex] << " at index " << minIndex <<
endl;

cout << "Maximum element is " << arr[maxIndex] << " at index " << maxIndex <<
endl;
30. Read 5 scores and show how much each score differs from the highest score .

int s[5], i;

int maxS;

cout << "Enter 5 s:" << endl;

for (i = 0; i < 5; i++) {

cin >> s[i];

maxS = s[0];

for (i = 1; i < 5; i++) {

if (s[i] > maxS) {

maxS = s[i];

cout << "\nDifference from the highest s (" << maxS << "):" << endl;

for (i = 0; i < 5; i++) {

cout << "S " << i + 1 << ": " << (maxS - s[i]) << endl;

You might also like