برمجة
برمجة
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.
By using arrays, programmers can manage data efficiently and write cleaner, more
organized code.
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
}
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.
Output: 5 -1 8 3 -1
Output: 2 7 6 11 1o
List[i] =2 *i +5;
If(i % 2==0)
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;
list[i] = i * i + 5;
if (i > 2)
myList[10];
Output:
0 -2
1 6
2 -12
3 1
4 13
Output:
List elements: 11 16 21 26 30
#include <iostream>
int main()
int count;
int alpha[5];
alpha[0] = 5;
alpha[count - 1] = alpha[count] - 4;
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];
one[j] = 5 * j + 3;
two[j] = 2 * one[j] - 1;
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
for(int i=0;i<=9;i++){
cin>>mumber[i];}
for(int i=0;i<=9;i++){
sum +=mumber[i];}
high =mumber[0];
for(int i=0;i<=9;i++){
if(high<mumber[i])
high=i;}
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.
for(int i=0;i<=9;i++){
cin>>A[i];
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.
int arr[SIZE];
int V, i;
cin >> V;
cin >> i;
cout << "Invalid index. Must be between 0 and 9." << endl;
return 1;
arr[i] = V;
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.
int summation=0
for(int i=0;i<size;i++){
summation +=A[i];
15. write function to find and return the index of the first largest element in an int
array.
int larg=A[0];
for(int i=0;i<size;i++){
if(larg <A[i]){
large=I;
16. Write a function to copy some or all of the elements of one array into another
array.
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
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
int small=A[0];
for(int i=0;i<size;i++){
if(small >A[i]){
small=I;
int lastIndex = 0;
maxVal = arr[i];
lastIndex = i;
lastIndex = i;
21. Write a program that prompts the user to input 10 characters and outputs the
characters in uppercase letters.
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.
for(int i=0;i<10;i++){
cin>>A[i];
cin>>num;
for(int i=0;i<10;i++){
if(num==A[i]){
freq++;
23. Write a program to find average of n element into int array except minimum
number.
24. Write a program to find position of specific element into int array.
cin >> n;
cout << "Enter " << n << " integers:" << endl;
if (arr[i] == target) {
position = i;
if (position != -1)
cout << "Element found at index: " << position << endl;
else
cin >> n;
z += x[i] * y[i];
26. Write a program to find product array of each element in array int x and y.
cin >> n;
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
int n, o = 0, e = 0;
cin >> n;
if (arr[i] % 2 == 0)
even[e++] = arr[i];
else
odd[o++] = arr[i];
for (int i = 0; i < o; i++) cout << odd[i] << " ";
for (int i = 0; i < e; i++) cout << even[i] << " ";
int arr[100], n, i;
cin >> n;
cout << "Enter " << n << " integers:" << endl;
minIndex = i;
maxIndex = i;
arr[minIndex] = arr[maxIndex];
arr[maxIndex] = temp;
cout << "Array after swapping min and max elements:" << endl;
}
29. Write program find the position of min and max element in one dimension array.
int arr[100], n, i;
cin >> n;
cout << "Enter " << n << " integers:" << endl;
minIndex = i;
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;
maxS = s[0];
maxS = s[i];
cout << "\nDifference from the highest s (" << maxS << "):" << endl;
cout << "S " << i + 1 << ": " << (maxS - s[i]) << endl;