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

C++ Examples Program

The document contains 10 questions related to C++ programming concepts like arrays, searching, sorting, matrices, and pairs. Each question provides sample code to solve a specific problem, like finding the sum or largest element of an array, reversing an array, searching for an element, multiplying matrices, and checking for pairs with a given sum. The code examples demonstrate basic C++ syntax along with the use of arrays, loops, conditional statements, and input/output operations to manipulate and process data.

Uploaded by

rida.maryam1215
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)
34 views

C++ Examples Program

The document contains 10 questions related to C++ programming concepts like arrays, searching, sorting, matrices, and pairs. Each question provides sample code to solve a specific problem, like finding the sum or largest element of an array, reversing an array, searching for an element, multiplying matrices, and checking for pairs with a given sum. The code examples demonstrate basic C++ syntax along with the use of arrays, loops, conditional statements, and input/output operations to manipulate and process data.

Uploaded by

rida.maryam1215
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/ 28

NTRODUCTION TO

PROGRAMMING

Question#1
Write a C++ program to find the sum of array?
#include<iostream>

using namespace std;

void main()

int array[10];

int sum = 0;

cout << "enter the value of which you want to find the sum" << endl;

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

cin >> array[i];

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

sum = sum + array[i];

cout << "sum of the elements is=" << sum;

cout << endl;

Output:

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

Question#2
Write a C++ program to reverse an array?

#include<iostream>

using namespace std;

void main()

int a[5];

cout << "enter the number" << endl;

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

cin >> a[i];

cout << "original array is" << endl;

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

cout << a[i] << " ";

cout << "reverse array is" << endl;

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

for (int i = 4 ; i >=0; i--)

cout << a[i] << " ";

Output:

Question#3
Write a C++ program to find the maximum number in an array?
#include<iostream>

using namespace std;

void main()

int array[10];

cout << "find the largest elemnts " << endl;

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

cin >> array[i];

}
RIDA FATIMA BSE223046
NTRODUCTION TO
PROGRAMMING

int largest = 0;

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

if (array[i] > largest)

largest = array[i];

cout << "largest elements' is" << largest << endl;

OUTPUT:

Question#4
Write a C++ program to find the linearly search an element in an array?

#include<iostream>

using namespace std;

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

void main()

int array[10];

cout << "enter the elemenst in an array " << endl;

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

cin >> array[i];

int search ;

cout << "enter the number you want to search" << endl;

cin >> search;

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

if (search == array[i])

cout << i+1;

OUTPUT:

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

Question#5
Write a C++ program to find the smallest and second smallest element in an
array?
#include<iostream>

using namespace std;

void small(int x[], int size);

int main()

int arr[10];

cout << "Enter the numbers" << endl;

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

cin >> arr[i];

small(arr, 10);

void small(int x[], int size)

int n = 0, y[10];

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

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

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

if (x[i] > x[j])

n = x[i];

x[i] = x[j];

x[j] = n;

cout << "sorted array is" << endl;

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

cout << x[i] << " ";

cout << endl;

cout << "Smallest element in the array is " << x[0];

cout << "Second smallest number in the array is " << x[1];

Output:

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

Question#6
Write a C++ program for median of two sorted arrays?
#include<iostream>

using namespace std;

int median(int x[], int size, int y[], int size1);

int main()

int a[5], b[6];

cout << "Enter the numbers of first array" << endl;

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

cin >> a[i];

cout << "Enter the numbers of second array" << endl;

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

cin >> b[i];

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

median(a, 5, b, 6);

int median(int x[], int size, int y[], int size1)

int c[11], p = 0, m, median;

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

c[i] = x[i];

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

c[size + i] = y[i];

cout << "Array is" << endl;

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

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

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

p = 0;

for (int j = i + 1; j < size + size1; j++)

if (c[i] > c[j])

p = c[i];

c[i] = c[j];

c[j] = p;

}
RIDA FATIMA BSE223046
NTRODUCTION TO
PROGRAMMING

cout << "sorted Array is" << endl;

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

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

m = (size + size1) / 2;

if ((size + size1) % 2 == 0)

cout << "Median is " << c[m];

else

median = c[m] + c[m + 1];

median = median / 2;

cout << "Median is " << median << endl;

return median;

Output:

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

Question#7
Write a C++ program to multiply two matrixes?
#include<iostream>

using namespace std;

void main()

int matrixA[2][2];

int matrixB[2][2];

int mult;

cout << "enter the value of matrix A" << endl;

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

for (int j = 0; j < 2; j++)

cin >> matrixA[i][j];

cout << endl;

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

cout << "enter the value of matrix A" << endl;

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

for (int j = 0; j < 2; j++)

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

cout << endl;

cout << endl;

cout << "enter the value of matrix B" << endl;

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

for (int j = 0; j < 2; j++)

cin >> matrixB[i][j];

cout << endl;

cout << endl;

cout << "enter the value of matrix B" << endl;

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

for (int j = 0; j < 2; j++)

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

}
RIDA FATIMA BSE223046
NTRODUCTION TO
PROGRAMMING

cout << endl;

cout << endl;

cout << "multiplication of matrix is" << endl;

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

for (int j = 0; j < 2; j++)

mult = matrixA[i][j] * matrixA[i][j];

cout << mult << " ";

cout << endl;

cout << endl;

Output:

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

Question#8
Write a C++ program for the majority element?
#include<iostream>

using namespace std;

void main()

int array[10];

cout << "enter the element" << endl;

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

cin >> array[i];

int majority = array[0];

int majoritycount = 1;

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

int count = 1;

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

if (array[i]==array[j])

count++;

if (majoritycount < count)

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

majority=array[i];

majoritycount=count;

cout << "majority elements=" << majority << endl;

cout<< "count=" << majoritycount << endl;

OUTPUT:

Question#9
Write a C++ program to find the number occurring odd number of times?
#include<iostream>

using namespace std;

void main()

int array[10];

cout << "enter the element" << endl;

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

cin >> array[i];

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

int odd = array[0];

int oddcount = 1;

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

int count = 1;

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

if (array[i]==array[j])

count++;

if ( count %2!=0 && oddcount < count)

odd=array[i];

oddcount=count;

cout << "majority elements=" << odd << endl;

cout<< "count=" << oddcount << endl;

OUTPUT:

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

Question#10
Write a C++ program for given an array and a number x, check for pair in A[] with
sum as x?
#include<iostream>

using namespace std;

void main()

int a[5];

int p = 0;

int y = 0;

cout << "enter the number" << endl;

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

cin >> a[i];

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

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

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

if (a[i] + a[j] == y)

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

p = 1;

if (p == 1)

cout << "yes it is pair" << endl;

else

cout << "no it is not pair" << endl;

Output:

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

Question#11
Write a C++ program to find the number occurring odd and number of times?
#include<iostream>

using namespace std;

void odd(int x[], int size);

int main()

int A[10];

cout << "Enter the numbers in the array" << endl;

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

cin >> A[i];

odd(A, 10);

void odd(int x[], int size)

int n,count=1,y[10];

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

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

if (x[i] > x[j])

n = x[i];

x[i] = x[j];

x[j] = n;

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

cout << "Sorted array is " << endl;

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

cout << x[i] << " ";

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

count = 1;

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

if (x[i]==x[j] && x[i]!=-1 && x[j] != -1)

count++;

y[i] = count;

x[j] = -1;

cout << endl;

cout << "the array is " << endl;

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

cout << x[i] << " ";

cout << endl;


RIDA FATIMA BSE223046
NTRODUCTION TO
PROGRAMMING

int p=0;

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

if (y[i] % 2 != 0 )

cout << x[i] << " occur odd number of times" << endl;

p = 1;

if (p == 0)

cout << "No number occurs odd number of time" << endl;

Output:

Question#12
Write a C++ program to find the missing number?
RIDA FATIMA BSE223046
NTRODUCTION TO
PROGRAMMING

#include<iostream>

using namespace std;

int main()

int A[10],n;

cout << "Enter the numbers" << endl;

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

cin >> A[i];

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

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

if (A[i] < A[j])

n = A[i];

A[i] = A[j];

A[j] = n;

cout << "Sorted array is" << endl;

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

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

cout << endl;

int p = 0;
RIDA FATIMA BSE223046
NTRODUCTION TO
PROGRAMMING

cout << "Missing nmuber is" << endl;

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

if (A[i] - A[i + 1] == 1)

p = 0;

else

cout << A[i] - 1 << " ";

p = 1;

if (p == 0)

cout << "There is no missing number" << endl;

OUPUT:

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

Question#13
Write a C++ program for largest sum contiguous subarray?
#include<iostream>

using namespace std;

int maxSubArraySum(int a[], int size)

int sum = a[0];

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

sum = sum + a[i];

return sum;

int main()

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

int a[] = { 1,2,3,4,5,8,9 };

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

int max_sum = maxSubArraySum(a, n);

cout << "maximum contiguous sum is= " << max_sum;

cout << endl;

return 0;

Output:

Question#14
Write a C++ program for merge an array of size n into another array of size m+n?
#include<iostream>

using namespace std;

void moveToEnd(int mPlusN[], int size)

#define NA -1

int j = size - 1;

for (int i = size - 1; i >= 0; i--)

if (mPlusN[i] != NA)

mPlusN[j] = mPlusN[i];

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

j--;

void merge(int mPlusN[], int N[], int m, int n)

int i = n;

int j = 0;

int k = 0;

while (k < (m + n))

if ((j == n) || (i < (m + n) && mPlusN[i] <= N[j]))

mPlusN[k] = mPlusN[i];

k++;

i++;

else

mPlusN[k] = N[j];

k++;

i++;

void printArray(int arr[], int size)

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

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

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

cout << endl;

int main()

int mPlusN[] = { 2, 8, NA,NA, NA, 13, NA, 15, 20 };

int N[] = { 5, 7, 9, 25 };

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

int m = sizeof(mPlusN) / sizeof(mPlusN[0]) - n;

moveToEnd(mPlusN, m + n);

merge(mPlusN, N, m, n);

printArray(mPlusN);

return 0;

Output:

Question#15
Write a C++ program to sort element by frequency?
#include<iostream>

using namespace std;

int main()

RIDA FATIMA BSE223046


NTRODUCTION TO
PROGRAMMING

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

const int n = sizeof(array) / sizeof(array[0]);

int freq[n] = { 0 };

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

freq[array[i]]++;

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

if (freq[i] != 0)

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

return 0;

Output:

RIDA FATIMA BSE223046

You might also like