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

Lab4_Array_Practice_Programs

The document contains a series of C++ programming exercises focusing on array manipulation. It includes tasks such as initializing arrays, calculating sums, reversing elements, finding the largest number, performing sequential searches, counting even and odd numbers, and checking if an array is sorted. Each exercise is accompanied by sample code demonstrating the implementation of the respective functionality.

Uploaded by

Shehar Bano
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab4_Array_Practice_Programs

The document contains a series of C++ programming exercises focusing on array manipulation. It includes tasks such as initializing arrays, calculating sums, reversing elements, finding the largest number, performing sequential searches, counting even and odd numbers, and checking if an array is sorted. Each exercise is accompanied by sample code demonstrating the implementation of the respective functionality.

Uploaded by

Shehar Bano
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 4: Arrays in C++

Q1. Initialize array with values 1 to 10 and print


#include <iostream>
using namespace std;

int main() {
int arr[10];

// Initialize with values 1 to 10


for (int i = 0; i < 10; i++) {
arr[i] = i + 1;
}

// Print array
cout << "Array elements: ";
for (int i = 0; i < 10; i++) {
cout << arr[i] << " ";
}

return 0;
}

Q2. Input 5 numbers and find their sum


#include <iostream>
using namespace std;

int main() {
int arr[5], sum = 0;

cout << "Enter 5 numbers: ";


for (int i = 0; i < 5; i++) {
cin >> arr[i];
sum += arr[i];
}

cout << "Sum = " << sum;

return 0;
}

Q3. Reverse array elements (no new array)


#include <iostream>
using namespace std;

int main() {
int arr[5];
cout << "Enter 5 numbers: ";
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}

cout << "Reversed array: ";


for (int i = 4; i >= 0; i--) {
cout << arr[i] << " ";
}

return 0;
}

Q4. Find largest number in array


#include <iostream>
using namespace std;

int main() {
int arr[8], max;

cout << "Enter 8 numbers: ";


for (int i = 0; i < 8; i++) {
cin >> arr[i];
}

max = arr[0];
for (int i = 1; i < 8; i++) {
if (arr[i] > max)
max = arr[i];
}

cout << "Largest number = " << max;

return 0;
}

Q5. Sequential search in array


#include <iostream>
using namespace std;

int main() {
int arr[5], key, found = 0;

cout << "Enter 5 numbers: ";


for (int i = 0; i < 5; i++) {
cin >> arr[i];
}
cout << "Enter number to search: ";
cin >> key;

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


if (arr[i] == key) {
cout << "Found at index " << i;
found = 1;
break;
}
}

if (!found)
cout << "Not found";

return 0;
}

Q6. Count even and odd numbers


#include <iostream>
using namespace std;

int main() {
int arr[6], even = 0, odd = 0;

cout << "Enter 6 numbers: ";


for (int i = 0; i < 6; i++) {
cin >> arr[i];
if (arr[i] % 2 == 0)
even++;
else
odd++;
}

cout << "Even numbers = " << even << endl;


cout << "Odd numbers = " << odd;

return 0;
}

Q7. Print array using pointer arithmetic


#include <iostream>
using namespace std;

int main() {
int arr[5] = {2, 4, 6, 8, 10};

cout << "Access using pointer notation:" << endl;


for (int i = 0; i < 5; i++) {
cout << *(arr + i) << " ";
}

return 0;
}

Q8. Input and print array in reverse order


#include <iostream>
using namespace std;

int main() {
int arr[6];

cout << "Enter 6 numbers: ";


for (int i = 0; i < 6; i++) {
cin >> arr[i];
}

cout << "Array in reverse order: ";


for (int i = 5; i >= 0; i--) {
cout << arr[i] << " ";
}

return 0;
}

Q9. Calculate average of array elements


#include <iostream>
using namespace std;

int main() {
int arr[6], sum = 0;
float avg;

cout << "Enter 6 numbers: ";


for (int i = 0; i < 6; i++) {
cin >> arr[i];
sum += arr[i];
}

avg = (float)sum / 6;
cout << "Average = " << avg;

return 0;
}

Q10. Check if array is sorted in ascending order


#include <iostream>
using namespace std;
int main() {
int arr[5];
bool isSorted = true;

cout << "Enter 5 numbers: ";


for (int i = 0; i < 5; i++) {
cin >> arr[i];
}

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


if (arr[i] > arr[i + 1]) {
isSorted = false;
break;
}
}

if (isSorted)
cout << "Array is sorted in ascending order.";
else
cout << "Array is NOT sorted.";

return 0;
}

You might also like