0% found this document useful (0 votes)
11 views46 pages

C++ Array and Matrix Operations Guide

Uploaded by

evieskye119
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views46 pages

C++ Array and Matrix Operations Guide

Uploaded by

evieskye119
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Name : Moti Saandh Phasko,

Paaadhuu Mutttuu Pottttuuuu


Practice Task No 1

#include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter size of array: ";

cin >> n;

int arr[n];

// input array elements

cout << "Enter " << n << " elements:\n";

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

cout << "Element " << i << ": ";

cin >> arr[i];

int key;

cout << "Enter the number to search for: ";

cin >> key;


// linear search

bool found = false;

int index = -1;

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

if (arr[i] == key) {

found = true;

index = i;

break; // stop at first match

if (found) {

cout << "Number found at index: " << index << endl;

} else {

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

return 0;

Practice Task No 2

#include <iostream>

using namespace std;

int main() {
int n;

cout << "Enter size of array: ";

cin >> n;

int arr[n];

// Input array elements (must be sorted)

cout << "Enter " << n << " elements in sorted order:\n";

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

cout << "Element " << i << ": ";

cin >> arr[i];

int key;

cout << "Enter the number to search for: ";

cin >> key;

int left = 0;

int right = n - 1;

int mid;

bool found = false;

// Binary Search

while (left <= right) {

mid = (left + right) / 2;

if (arr[mid] == key) {

found = true;

break; // element found


} else if (arr[mid] < key) {

left = mid + 1; // search right side

} else {

right = mid - 1; // search left side

if (found) {

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

else {

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

return 0;

Practice Task No 3

#include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter size of array: ";

cin >> n;

int arr[n];
// input the array elements

cout << "Enter " << n << " elements:\n";

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

cout << "Element " << i << ": ";

cin >> arr[i];

int order;

cout << "Enter 1 for Ascending order or 2 for Descending order: ";

cin >> order;

// Bubble Sort

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

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

if (order == 1) { // ascending

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

else if (order == 2) { // descending

if (arr[j] < arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}
}

// Display sorted array

cout << "\nSorted Array:\n";

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

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

cout << endl;

return 0;

Practice Task No 4

#include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter size of array: ";

cin >> n;

int arr[n];

// input the array elements


cout << "Enter " << n << " elements:\n";

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

cout << "Element " << i << ": ";

cin >> arr[i];

// initialize both largest and smallest

int largest = arr[0];

int smallest = arr[0];

// find largest and smallest

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

if (arr[i] > largest) {

largest = arr[i];

if (arr[i] < smallest) {

smallest = arr[i];

cout << "\nLargest element = " << largest << endl;

cout << "Smallest element = " << smallest << endl;

return 0;

Practice Task No 5
#include <iostream>

using namespace std;

int main() {

const int rows = 3;

const int cols = 2;

int A[rows][cols];

int B[rows][cols];

int C[rows][cols];

// Input Matrix A

cout << "Enter elements of Matrix A (3 x 2): " << endl;

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

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

cout << "A[" << i << "][" << j << "]: ";

cin >> A[i][j];

// Input Matrix B

cout << "\nEnter elements of Matrix B (3 x 2): " << endl;

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

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

cout << "B[" << i << "][" << j << "]: ";

cin >> B[i][j];

}
// Calculate C = A + B

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

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

C[i][j] = A[i][j] + B[i][j];

// Display all three matrices

cout << "\nMatrix A:\n";

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

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

cout << A[i][j] << " ";

cout << endl;

cout << "\nMatrix B:\n";

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

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

cout << B[i][j] << " ";

cout << endl;

cout << "\nMatrix C (A + B):\n";

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

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

cout << C[i][j] << " ";


}

cout << endl;

return 0;

Practice Task No 6
#include <iostream>

#include <iomanip>

using namespace std;

int main() {

string name;

float t1, t2, t3;

float attendance;

cout << "Enter student's name: ";

getline(cin, name);

cout << "Enter test score 1: ";

cin >> t1;

cout << "Enter test score 2: ";

cin >> t2;

cout << "Enter test score 3: ";

cin >> t3;

cout << "Enter attendance percentage: ";


cin >> attendance;

// Calculate final grade

float testAvg = (t1 + t2 + t3) / 3.0;

float finalGrade = (testAvg * 0.70) + (attendance * 0.30);

cout << "\n----------------------------------------------\n";

cout << setw(15) << left << "Name"

<< setw(10) << "Test 1"

<< setw(10) << "Test 2"

<< setw(10) << "Test 3"

<< setw(12) << "Attendance"

<< setw(10) << "Final" << endl;

cout << "----------------------------------------------\n";

cout << setw(15) << left << name

<< setw(10) << t1

<< setw(10) << t2

<< setw(10) << t3

<< setw(12) << attendance

<< setw(10) << finalGrade << endl;

return 0;

Consider an integer array, the number of elements should be


determined by the user. The elements are also taken as input from
the user. Write a program to print sum, an average of all numbers, the
smallest and largest element of an array.
#include <iostream>

using namespace std;

int main()

int n;

cout << "Enter size of array: ";

cin >> n;

int arr[n];

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

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

cout << "Element " << i << ": ";

cin >> arr[i];

int sum = 0;

int smallest = arr[0];

int largest = arr[0];

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

sum += arr[i];

if (arr[i] < smallest)

smallest = arr[i];
if (arr[i] > largest)

largest = arr[i];

float average = (float)sum / n;

cout << "\nSum of all numbers = " << sum << endl;

cout << "Average of all numbers = " << average << endl;

cout << "Smallest number = " << smallest << endl;

cout << "Largest number = " << largest << endl;

return 0;

Take an array of 10 elements. Split it into the middle and store the
elements in two different arrays. For details see the
following program.
#include <iostream>

using namespace std;

int main()

const int size = 10;

int arr[size];

int first[5];

int second[5];

// input array elements


cout << "Enter 10 elements:\n";

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

cout << "Element " << i << ": ";

cin >> arr[i];

// split array into 2 halves

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

first[i] = arr[i];

second[i] = arr[i + 5];

cout << "\nInitial array:\n";

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

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

cout << "\n\nAfter splitting:\n";

cout << "First array : ";

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

cout << first[i] << " ";

cout << "\nSecond array: ";


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

cout << second[i] << " ";

cout << endl;

return 0;

Take 20 integer inputs from user and print the following:


number of positive numbers
number of negative numbers
number of odd numbers
number of even numbers
number of 0.
#include <iostream>

using namespace std;

int main() {

int num;

int positiveCount = 0, negativeCount = 0, oddCount = 0, evenCount = 0, zeroCount = 0;

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

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

cin >> num;


// Count positive, negative, and zero

if (num > 0) {

positiveCount++;

} else if (num < 0) {

negativeCount++;

} else {

zeroCount++;

// Count odd and even numbers

if (num != 0) {

if (num % 2 == 0) {

evenCount++;

} else {

oddCount++;

cout << "Number of positive numbers: " << positiveCount << endl;

cout << "Number of negative numbers: " << negativeCount << endl;

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

cout << "Number of even numbers: " << evenCount << endl;

cout << "Number of zeros: " << zeroCount << endl;

return 0;

}
Write a program that allows the user to input a series of numbers
until they enter a negative number. Store these numbers in an array
and then find and print the sum of all the positive numbers entered.
#include <iostream>

using namespace std;

int main() {

const int MAX_SIZE = 1000; // Maximum size of array

int numbers[MAX_SIZE];

int count = 0;

int num;

cout << "Enter numbers (enter a negative number to stop):" << endl;

// Input numbers until a negative number is entered

while (true) {

cin >> num;

if (num < 0) {

break;

numbers[count] = num;

count++;

// Calculate sum of positive numbers

int sum = 0;

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

sum += numbers[i]; // Since all numbers in the array are positive

}
cout << "Sum of all positive numbers entered: " << sum << endl;

return 0;

Design a program that takes 5 integer inputs from the user and stores
them in an array. Find and print the frequency of each unique
number in the array.
#include <iostream>

using namespace std;

int main() {

const int SIZE = 5;

int arr[SIZE];

int freq[SIZE] = {0}; // Frequency array initialized to 0

bool counted[SIZE] = {false}; // To keep track of already counted numbers

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

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

cin >> arr[i];

// Calculate frequency of each unique number

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

if (!counted[i]) { // Only count if this number hasn't been counted

int count = 1;

for (int j = i + 1; j < SIZE; j++) {

if (arr[i] == arr[j]) {
count++;

counted[j] = true; // Mark as counted

cout << "Number " << arr[i] << " occurs " << count << " time(s)" << endl;

return 0;

Create a program that takes 10 integer inputs from the user and
stores them in an array. Find and print the second smallest and
second largest numbers in the array.
#include <iostream>

using namespace std;

int main() {

const int SIZE = 10;

int arr[SIZE];

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

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

cin >> arr[i];

int smallest, secondSmallest, largest, secondLargest;

// Initialize smallest and largest with the first element


smallest = secondSmallest = arr[0];

largest = secondLargest = arr[0];

// Find smallest and second smallest

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

if (arr[i] < smallest) {

secondSmallest = smallest;

smallest = arr[i];

} else if (arr[i] < secondSmallest && arr[i] != smallest) {

secondSmallest = arr[i];

if (arr[i] > largest) {

secondLargest = largest;

largest = arr[i];

} else if (arr[i] > secondLargest && arr[i] != largest) {

secondLargest = arr[i];

cout << "Second smallest number: " << secondSmallest << endl;

cout << "Second largest number: " << secondLargest << endl;

return 0;

Ask the user to enter the size of an array (N). Then, ask them to enter
N integers. Check if the array is sorted in ascending order and display
an appropriate message. (if it’s not sorted, then sort it using the
bubble sort method)
#include <iostream>

using namespace std;

int main() {

int N;

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

cin >> N;

int arr[N];

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

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

cin >> arr[i];

// Check if the array is sorted in ascending order

bool sorted = true;

for (int i = 0; i < N - 1; i++) {

if (arr[i] > arr[i + 1]) {

sorted = false;

break;

if (sorted) {

cout << "The array is already sorted in ascending order." << endl;

} else {
cout << "The array is not sorted. Sorting using Bubble Sort..." << endl;

// Bubble Sort

for (int i = 0; i < N - 1; i++) {

for (int j = 0; j < N - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

// Swap arr[j] and arr[j + 1]

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

cout << "The sorted array is: ";

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

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

cout << endl;

return 0;

Imagine you work for a retail company, and you are responsible for
generating a sales report. You have a list of products sold, including
the product name, quantity sold, and price per unit. Write a program
that calculates the total revenue for each product and the overall
total revenue. Format the output using setw to display
the report neatly.
#include <iostream>

#include <iomanip> // For setw

using namespace std;

int main() {

const int SIZE = 5; // Number of products (you can change as needed)

string productName[SIZE];

int quantitySold[SIZE];

double pricePerUnit[SIZE];

double revenue[SIZE];

double totalRevenue = 0;

// Input product details

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

cout << "Enter details for product " << i + 1 << ":\n";

cout << "Product Name: ";

cin >> productName[i];

cout << "Quantity Sold: ";

cin >> quantitySold[i];

cout << "Price per Unit: ";

cin >> pricePerUnit[i];

revenue[i] = quantitySold[i] * pricePerUnit[i];

totalRevenue += revenue[i];

cout << endl;

}
// Print the sales report

cout << left << setw(15) << "Product"

<< right << setw(15) << "Quantity"

<< setw(15) << "Price/unit"

<< setw(20) << "Revenue" << endl;

cout << string(65, '-') << endl; // Separator line

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

cout << left << setw(15) << productName[i]

<< right << setw(15) << quantitySold[i]

<< setw(15) << fixed << setprecision(2) << pricePerUnit[i]

<< setw(20) << fixed << setprecision(2) << revenue[i]

<< endl;

cout << string(65, '-') << endl;

cout << left << setw(45) << "Total Revenue:"

<< setw(20) << fixed << setprecision(2) << totalRevenue << endl;

return 0;

Design a program that manages student enrollment in courses. The


program should allow users to add students to courses and display a
list of enrolled students for each course. Use loops and conditional
statements to achieve this.
#include <iostream>

#include <string>
using namespace std;

int main() {

const int MAX_COURSES = 5;

const int MAX_STUDENTS = 50;

string courses[MAX_COURSES];

string students[MAX_COURSES][MAX_STUDENTS];

int studentCount[MAX_COURSES] = {0};

int courseCount;

cout << "Enter number of courses: ";

cin >> courseCount;

[Link](); // Clear newline character

// Input course names

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

cout << "Enter name of course " << i + 1 << ": ";

getline(cin, courses[i]);

int choice;

do {

cout << "\n--- Menu ---\n";

cout << "1. Add student to a course\n";

cout << "2. Display enrolled students\n";

cout << "3. Exit\n";

cout << "Enter your choice: ";

cin >> choice;


[Link](); // Clear newline

if (choice == 1) {

string courseName, studentName;

cout << "Enter course name: ";

getline(cin, courseName);

// Find course index

int index = -1;

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

if (courses[i] == courseName) {

index = i;

break;

if (index == -1) {

cout << "Course not found!" << endl;

} else {

if (studentCount[index] >= MAX_STUDENTS) {

cout << "Course is full!" << endl;

} else {

cout << "Enter student name: ";

getline(cin, studentName);

students[index][studentCount[index]] = studentName;

studentCount[index]++;

cout << "Student added successfully!" << endl;

}
} else if (choice == 2) {

cout << "\n--- Enrolled Students ---\n";

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

cout << "Course: " << courses[i] << endl;

if (studentCount[i] == 0) {

cout << " No students enrolled." << endl;

} else {

for (int j = 0; j < studentCount[i]; j++) {

cout << " " << students[i][j] << endl;

} else if (choice != 3) {

cout << "Invalid choice. Try again!" << endl;

} while (choice != 3);

cout << "Exiting program." << endl;

return 0;

Write a C++ program that asks the user to input an N number of


values in an array (means, the size of the array as well as all the values
should be input by the user) and then displays those values
in reverse order.
#include <iostream>

using namespace std;


int main() {

int N;

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

cin >> N;

int arr[N];

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

cout << "Enter value number " << i+1 << ": ";

cin >> arr[i];

cout << "The numbers in reverse order are:" << endl;

for (int i = N-1; i >= 0; i--) {

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

return 0;

Write a program that should ask the user to input the size of an array
and then take that much number of values as input from the user and
save them in the array. Finally, print the sum of all those
values of the array.

#include <iostream>

using namespace std;


int main() {

int size;

// Ask the user for the size of the array

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

cin >> size;

// Create an array of the given size

int* arr = new int[size]; // Using dynamic array

// Take input for the array elements

cout << "Enter " << size << " values:" << endl;

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

cin >> arr[i];

// Calculate the sum of array elements

int sum = 0;

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

sum += arr[i];

// Print the sum

cout << "The sum of the array elements is: " << sum << endl;

// Free dynamically allocated memory

delete[] arr;
return 0;

Write a program to declare and initialize an array with 10 values


(means give the values to array elements within the code, no need to
take input from the user) and then display the average of the
values of that array.
#include <iostream>

using namespace std;

int main() {

// Declare and initialize an array with 10 values

int arr[10] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50};

// Variable to store the sum

int sum = 0;

// Calculate the sum of array elements

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

sum += arr[i];

// Calculate the average

double average = sum / 10.0; // divide by 10.0 to get a floating-point result

// Display the average

cout << "The average of the array elements is: " << average << endl;
return 0;

Write a program to copy the elements of one array into another array.
And then, print the values of both of the arrays. You may give fixed
values to the first array elements (means, it is not necessary to get
input from the user).
#include <iostream>

using namespace std;

int main() {

// First array with fixed values

int firstArray[] = {3, 41, 88, 11, 2, 69};

int size = sizeof(firstArray) / sizeof(firstArray[0]);

// Second array to copy values into

int secondArray[size];

// Copying values from firstArray to secondArray

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

secondArray[i] = firstArray[i];

// Displaying values of the first array

cout << "The values of the first array are:\n";

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

cout << firstArray[i] << " ";

}
cout << "\nThe values of the second array are:\n";

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

cout << secondArray[i] << " ";

cout << endl;

return 0;

Write a program that gets array size and elements as input from a
user and then prints only the EVEN values that are
present in the array.
#include <iostream>

using namespace std;

int main() {

int size;

// Ask user for array size

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

cin >> size;

int arr[size]; // Declare array with user-defined size

// Take input values

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

cout << "Enter value number " << i + 1 << ": ";

cin >> arr[i];

}
// Display even numbers

cout << "The EVEN numbers are:\n";

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

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

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

cout << endl;

return 0;

Write a program that gets array size and elements as input from a
user and then prints only those values that are present on ODD
indices (whose index number is ODD).

#include <iostream>

using namespace std;

int main() {

int size;

// Get array size from user

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

cin >> size;

int arr[size]; // Declare array with given size


// Get array elements from user

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

cout << "Enter value number " << i + 1 << ": ";

cin >> arr[i];

// Display values at odd indices

cout << "The values on the ODD indices are:\n";

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

if (i % 2 != 0) { // check if index is odd

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

cout << endl;

return 0;

Write a program that gets the array size and elements as input from
the user. Then, create another array of the same size as the first array
and copy the values of the first array in reverse order in the second
array (means the value on the first index of the first array should be
saved on the last index of the second array, and so on). Then, display
the values of both arrays
#include <iostream>

using namespace std;

int main() {

int size;
// Get size of the array from user

cout << "Enter size of array: ";

cin >> size;

int firstArray[size];

int secondArray[size];

// Get elements for the first array

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

cout << "Enter value " << i + 1 << ": ";

cin >> firstArray[i];

// Copy elements in reverse order to the second array

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

secondArray[i] = firstArray[size - 1 - i];

// Display values of the first array

cout << "\nValues of the first array are\n";

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

cout << firstArray[i] << " ";

// Display values of the second (reversed) array

cout << "\n\nValues of the second array are\n";

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

cout << secondArray[i] << " ";


}

cout << endl;

return 0;

Write a program where you should sum the elements of two same-
sized arrays and the result should be saved in the third array. Then,
print the values of all of the three arrays.
The values of the first two arrays may be given fixed (no need to get
input) but each value of the third array should be the sum of
respective values from the first and the second arrays.

#include <iostream>

using namespace std;

int main() {

// Define two arrays with fixed values

int firstArray[] = {21, 8, 11, 4, 33, 50};

int secondArray[] = {7, 35, 6, 23, 14, 10};

int thirdArray[6];

// Calculate the sum of the two arrays and store in the third array

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

thirdArray[i] = firstArray[i] + secondArray[i];

// Display the arrays


cout << "The values of the first array are:\n";

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

cout << firstArray[i] << " ";

cout << "\nThe values of the second array are:\n";

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

cout << secondArray[i] << " ";

cout << "\nThe values of the third array are:\n";

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

cout << thirdArray[i] << " ";

cout << endl;

return 0;

Write a program that creates two integer arrays of sizes N and M,


respectively. These arrays can be initialized within the code (no need
to get values as input from the user). Then, create a third array of size
N+M (i.e., whose size should be equal to the sum of the sizes of the
first and the second array) and copy the elements of both (first and
second) arrays into the third array. Finally, print the values of all
the three arrays
#include <iostream>

using namespace std;


int main() {

// Initialize two arrays with fixed values

int firstArray[] = {23, 10, 15, 7, 42};

int secondArray[] = {45, 77, 12, 89, 53, 20};

// Determine their sizes

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

int m = sizeof(secondArray) / sizeof(secondArray[0]);

// Create a third array of size n + m

int thirdArray[n + m];

// Copy elements from the first array

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

thirdArray[i] = firstArray[i];

// Copy elements from the second array

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

thirdArray[n + i] = secondArray[i];

// Display the arrays

cout << "Values of the first array are\n";

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

cout << firstArray[i] << " ";

cout << "\n\nValues of the second array are\n";


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

cout << secondArray[i] << " ";

cout << "\n\nValues of the third array are\n";

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

cout << thirdArray[i] << " ";

cout << endl;

return 0;

Write a program that handles students’ records/mark sheets. First,


ask the user to input the number of students for which records are to
be entered (this value will be used as the size of different arrays to be
created in the next steps). Then, create 8 arrays having the size gotten
in the previous step.
#include <iostream>

#include <iomanip>

using namespace std;

int main() {

int n;

cout << "Enter number of students: ";

cin >> n;

string name[n];

int prog[n], math[n], eng[n], ps[n];


int total[n];

float percent[n];

char grade[n];

// Input names

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

cout << "\nEnter name of student " << i + 1 << ": ";

cin >> name[i];

// Input marks

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

cout << "\nEnter marks for " << name[i] << ":\n";

cout << "Programming: ";

cin >> prog[i];

cout << "Math: ";

cin >> math[i];

cout << "English: ";

cin >> eng[i];

cout << "P.S.: ";

cin >> ps[i];

// Calculate totals and percentage

total[i] = prog[i] + math[i] + eng[i] + ps[i];

percent[i] = (total[i] / 400.0) * 100;

// Assign grade

if (percent[i] >= 80)

grade[i] = 'A';
else if (percent[i] >= 70)

grade[i] = 'B';

else if (percent[i] >= 60)

grade[i] = 'C';

else

grade[i] = 'F';

// Display results

cout << "\


n==========================================================================\n";

cout << left << setw(10) << "Name"

<< setw(12) << "Programming"

<< setw(8) << "Math"

<< setw(10) << "English"

<< setw(8) << "P.S."

<< setw(12) << "Total"

<< setw(12) << "Percentage"

<< setw(6) << "Grade" << endl;

cout << "==========================================================================\


n";

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

cout << left << setw(10) << name[i]

<< setw(12) << prog[i]

<< setw(8) << math[i]

<< setw(10) << eng[i]

<< setw(8) << ps[i]

<< setw(12) << total[i]


<< setw(12) << fixed << setprecision(2) << percent[i]

<< setw(6) << grade[i] << endl;

cout << "==========================================================================\


n";

return 0;

Write a program that gets array elements as input from a user and
then prints only the smallest value.
#include <iostream>

using namespace std;

int main() {

int size;

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

cin >> size;

int arr[size];

// Get array values from user

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

cout << "Enter value number " << i + 1 << ": ";

cin >> arr[i];

// Find the smallest value


int smallest = arr[0];

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

if (arr[i] < smallest) {

smallest = arr[i];

// Display the smallest value

cout << "The smallest value is: " << smallest << endl;

return 0;

Write a program that gets array elements as input from a user and
then prints only the largest value.
#include <iostream>

using namespace std;

int main() {

int size;

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

cin >> size;

int arr[size];

// Get array values from user

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


cout << "Enter value number " << i + 1 << ": ";

cin >> arr[i];

// Find the largest value

int largest = arr[0];

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

if (arr[i] > largest) {

largest = arr[i];

// Display the largest value

cout << "The largest value is: " << largest << endl;

return 0;

Write a program that gets array elements as input from a user and then asks the user to input
any value to search from the array. After that, if the value entered by the user is present in
the array, it should display the index where that value is present. If the value is NOT present
in the array, then it should print a message saying that the given value is not
present in the array.

#include <iostream>

using namespace std;

int main() {

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

cin >> size;

int arr[size];

// Get array values from user

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

cout << "Enter value number " << i + 1 << ": ";

cin >> arr[i];

// Get value to search

int searchValue;

cout << "Enter the value which want to search from this array: ";

cin >> searchValue;

// Search for the value

bool found = false;

int index = -1;

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

if (arr[i] == searchValue) {

found = true;

index = i;

break; // stop after finding the first match

// Display result
if (found)

cout << searchValue << " is present at the " << index << " index" << endl;

else

cout << searchValue << " is NOT present in the array" << endl;

return 0;

You might also like