0% found this document useful (0 votes)
10 views8 pages

Ya War 39785 Array PF

The document contains a series of C++ programming assignments by Yawar Mujtaba focused on array manipulation. It includes programs to reverse an array, find the minimum value, search for an element's index, check for the presence of a number, count even and odd numbers, and insert an element at a specific position in an array. Each assignment is accompanied by the corresponding C++ code implementation.

Uploaded by

yawarmujtaba1
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)
10 views8 pages

Ya War 39785 Array PF

The document contains a series of C++ programming assignments by Yawar Mujtaba focused on array manipulation. It includes programs to reverse an array, find the minimum value, search for an element's index, check for the presence of a number, count even and odd numbers, and insert an element at a specific position in an array. Each assignment is accompanied by the corresponding C++ code implementation.

Uploaded by

yawarmujtaba1
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/ 8

NAME:- YAWAR MUJTABA

ID NO:- 39785
ASSIGNMENT :- ARRAY

1.write a c++ program to reverse an array of integer value .


Ans: #include
<iostream> using

namespace std; int

main() {

int arr[] = {1, 2, 3, 4, 5}; int

size = sizeof(arr) / sizeof(arr[0]);

int start = 0; int end = size - 1;

while (start < end) { int temp

= arr[start]; arr[start] =

arr[end]; arr[end] = temp;

start++; end--;

}
cout<<"before reverse = "<<"1 2 3 4

5"<<endl; cout << "Reversed Array: ";

for (int i = 0; i < size; i++) { cout << arr[i]

<< " ";

}
return 0;
}
NAME:- YAWAR MUJTABA
ID NO:- 39785
ASSIGNMENT :- ARRAY
2. .write a c++ program to find the minimum value of an array.
Ans: #include
<iostream> using

namespace std; int

main() {

int size;
cout << "Enter the size of the

array: "; cin >> size; int arr[size];

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


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

cin >> arr[i];

}
int min = arr[0]; for

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

if (arr[i] < min) {

min = arr[i];

}
}
cout << "Minimum Value: " << min << endl;
return 0;
}
NAME:- YAWAR MUJTABA
ID NO:- 39785
ASSIGNMENT :- ARRAY

3. .write a c++ program to the find the index of an array element?


Ans: #include<iostream> using
namespace std; int main() { int

arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int n = sizeof(arr) / sizeof(arr[0]);

int x;

cout << "Enter the element to

search: "; cin >> x; for(int i = 0; i <

n; i++) { if(arr[i] == x) {

cout << "Element found at index: " << i;


return 0;
}
}
cout << "Element not found";

return 0;

}
NAME:- YAWAR MUJTABA
ID NO:- 39785
ASSIGNMENT :- ARRAY

4. Take 10 integers input from the user and store the in array . Again ask the
user to give a number now , tell the user whether that number is present in
array or not.
Ans:
#include<iostream>

using namespace std;

int main() {

int arr[10]; cout <<

"Enter 10 integers: ";

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

cin >> arr[i];

}
int num; cout << "Enter a

number to search: "; cin >> num;

bool found = false; for(int i = 0; i <

10; i++) { if(arr[i] == num) {

found = true; break;

}
}
if(found)
{
NAME:- YAWAR MUJTABA
ID NO:- 39785
ASSIGNMENT :- ARRAY
cout << "Number is present in the array.";
} else {
cout << "Number is not present in the array.";
}
return 0;
}

5.write a c++ program to input elements in an array from user and count even
and odd in the array?
Ans:
#include<iostream> using

namespace std; int main() {

int arr[10]; cout << "Enter

10 integers: "; int even =

0, odd = 0; for(int i = 0; i <

10; i++) { cin >> arr[i];

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

even++;
NAME:- YAWAR MUJTABA
ID NO:- 39785
ASSIGNMENT :- ARRAY
}
Else
{

odd++;

}
}

cout << "Number of even numbers: " << even <<

endl; cout << "Number of odd numbers: " << odd <<

endl;

return 0;
}

6.write a c++ program to insert elements in an array at specific position?


Ans:
#include<iostream> using namespace

std; int main() { int arr[10] = {1, 2,

3, 4, 5, 6, 7, 8, 9, 10}; int pos, val;


NAME:- YAWAR MUJTABA
ID NO:- 39785
ASSIGNMENT :- ARRAY
cout << "Original array: "; for(int i =

0; i < 10; i++) { cout << arr[i] << "

";

}
cout << endl; cout << "Enter the position where you want

to insert an element: "; cin >> pos; cout << "Enter the value

to insert: "; cin >> val;

// Shift elements to the right to make space for the new element
for(int i = 9; i >= pos; i--) {

arr[i] = arr[i - 1];

}
arr[pos - 1] = val; cout <<

"Array after insertion: ";

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

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

}
cout << endl;

return 0;
}
NAME:- YAWAR MUJTABA
ID NO:- 39785
ASSIGNMENT :- ARRAY

You might also like