0% found this document useful (0 votes)
65 views21 pages

WameedMUCLecture 2021 92910486

Uploaded by

dev.esraa23
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)
65 views21 pages

WameedMUCLecture 2021 92910486

Uploaded by

dev.esraa23
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/ 21

Computer Skills & Programming II Dr.

Shayma Akram

Lecture 7: Exercises, Practice, Solution

Arrays
Write a program to print array elements using recursion

#include <iostream>
using namespace std;

//Function declaration
void PrintArray(int arr[], int start, int len);

int main()
{
int MAX_SIZE = 100;
int arr[MAX_SIZE];
int num, i;

// Inputting size and elements in array


cout << "Enter size of the array: ";
cin >> num;

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


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

// Printing array recursively


cout << "Elements in the array: ";
PrintArray(arr, 0, num);

return 0;
}

// Printing array recursively within a given range.


void PrintArray(int arr[], int start, int len)
{
Computer Skills & Programming II Dr. Shayma Akram

// Recursion base condition


if (start >= len)
return;

// Printing the current array element


cout << arr[start] << "\t";

// Recursively calling printArray to print next element in the array


PrintArray(arr, start + 1, len);
}

Example

Input

Enter size of the array: 5


Enter elements in the array: 1 2 3 4 5
Elements in the array: 1 2 3 4 5

Write a C++ program to find the largest three elements in an array.

#include<iostream>
using namespace std;

void three_largest(int arr[], int arr_size)


{
int i, first, second, third;

if (arr_size < 3)
{
cout << "Invalid Input";
}

third = first = second = INT_MIN;


for (i = 0; i < arr_size ; i ++)
{
if (arr[i] > first)
Computer Skills & Programming II Dr. Shayma Akram

{
third = second;
second = first;
first = arr[i];
}
else if (arr[i] > second)
{
third = second;
second = arr[i];
}

else if (arr[i] > third)


third = arr[i];
}

cout << "\nThree largest elements are: " <<first <<", "<< second <<", "<< t
hird;
}
int main()
{
int nums[] = {7, 12, 9, 15, 19, 32, 56, 70};
int n = sizeof(nums)/sizeof(nums[0]);
cout << "Original array: ";
for (int i=0; i < n; i++)
cout << nums[i] <<" ";
three_largest(nums, n);
return 0;
}

Sample Output:

Original array: 7 12 9 15 19 32 56 70
Three largest elements are: 70, 56, 32
Computer Skills & Programming II Dr. Shayma Akram
Computer Skills & Programming II Dr. Shayma Akram

Write a C++ program to find the second smallest elements in a given array of integers.

#include <iostream>
using namespace std;
int find_Second_Smallest(int array_num[], int n) {
int smallest_num, second_smallest_num;
if (array_num[0] < array_num[1]) {
smallest_num = array_num[0];
second_smallest_num = array_num[1];
} else {
smallest_num = array_num[1];
second_smallest_num = array_num[0];
}
for (int i = 0; i < n; i++) {
if (smallest_num > array_num[i]) {
second_smallest_num = smallest_num;
smallest_num = array_num[i];
} else if (array_num[i] < second_smallest_num && array_num[i] > smallest_num) {
second_smallest_num = array_num[i];
}
}
return second_smallest_num;
}
int main() {
int n = 7;
int array_num[7] = {
5,
6,
7,
2,
3,
4,
12
};
int s = sizeof(array_num) / sizeof(array_num[0]);
cout << "Original array: ";
Computer Skills & Programming II Dr. Shayma Akram

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


cout << array_num[i] <<" ";
int second_smallest_num = find_Second_Smallest(array_num, n);
cout<<"\nSecond smallest number: "<<second_smallest_num;
return 0;
}

Write a C++ program to find the most occurring element in an array of integers.

#include<iostream>
using namespace std;

void most_occurred_number(int nums[], int size)


{
int max_count = 0;
cout << "\nMost occurred number: ";
for (int i=0; i<size; i++)
{
int count=1;
Computer Skills & Programming II Dr. Shayma Akram

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


if (nums[i]==nums[j])
count++;
if (count>max_count)
max_count = count;
}

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


{
int count=1;
for (int j=i+1;j<size;j++)
if (nums[i]==nums[j])
count++;
if (count==max_count)
cout << nums[i] << endl;
}
}

int main()
{
int nums[] = {4, 5, 9, 12, 9, 22, 45, 7};
int n = sizeof(nums)/sizeof(nums[0]);
cout << "Original array: ";
for (int i=0; i < n; i++)
cout << nums[i] <<" ";
most_occurred_number(nums, n);
}

Sample Output:

Original array: 4 5 9 12 9 22 45 7
Most occurred number: 9
Computer Skills & Programming II Dr. Shayma Akram

Write a C++ program to update every array element by multiplication of next and
previous values of a given array of integers.
Computer Skills & Programming II Dr. Shayma Akram

#include<iostream>
using namespace std;

void replace_elements(int nums[], int n)


{
if (n <= 1)
return;

int prev_element = nums[0];


nums[0] = nums[0] * nums[1];

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


{
int curr_element = nums[i];

nums[i] = prev_element * nums[i+1];

prev_element = curr_element;
}

nums[n-1] = prev_element * nums[n-1];


}

int main()
{
int nums[] = {0, 1, 3, 4, 5, 6, 7, 8, 10};
int n = sizeof(nums)/sizeof(nums[0]);
cout << "Original array: ";
for (int i=0; i < n; i++)
cout << nums[i] <<" ";
replace_elements(nums,n);
cout << "\nNew array elements: ";
for (int i=0; i < n; i++)
cout << nums[i] <<" ";
return 0;

}
Sample Output:

Original array: 0 1 3 4 5 6 7 8 10
New array elements: 0 0 4 15 24 35 48 70 80
Computer Skills & Programming II Dr. Shayma Akram

Two-dimensional array
A good representation of a 2-dimensional array is a grid because, technically, it is one. A practical
application for 2-dimensional arrays would be to use them to store the available seats in a cinema.
Here's a visual representation of what I'm referring to:

0 means the seat is available,


1 means that isn't.
Computer Skills & Programming II Dr. Shayma Akram

#include <iostream >


using namespace std;
int main(int argc, char **argv)
{

// declaration
int i, j;
int cinema[5][5];
// filling with zeros
for (j = 0; j < 5; j++)
for (i = 0; i < 5; i++)
cinema[j][i] = 0;
// filling with data
cinema[2][2] = 1; // center
for (i = 1; i < 4; i++) // fourth row
{
cinema[i][3] = 1;
}
for (i = 0; i < 5; i++) // the last row
{
cinema[i][4] = 1;
}

for (j = 0; j < 5; j++)


{
for (i = 0; i < 5; i++)
{
cout << ("%d", cinema[i][j]);
}
cout << ("\n");
}
return (EXIT_SUCCESS);
}
Computer Skills & Programming II Dr. Shayma Akram

Loops

Write a program in C++ to display n terms of natural number and their sum.

#include <iostream>
using namespace std;
int main()
{
int n,i,sum=0;
cout << "\n\n Display n terms of natural number and their sum:\n";
cout << "---------------------------------------\n";
cout << " Input a number of terms: ";
cin>> n;
cout << " The natural numbers upto "<<n<<"th terms are: \n";
for (i = 1; i <= n; i++)
{
cout << i << " ";
sum=sum+i;
}
cout << "\n The sum of the natural numbers is: "<<sum << endl;
}

Sample Output:

Display n terms of natural number and their sum:


---------------------------------------
Input a number of terms: 7
The natural numbers upto 7th terms are:
1 2 3 4 5 6 7
The sum of the natural numbers is: 28
Computer Skills & Programming II Dr. Shayma Akram

Write a program in C++ to check whether a number is prime or not.


Computer Skills & Programming II Dr. Shayma Akram

#include <iostream>
using namespace std;
int main()
{
int num1, ctr = 0;
cout << "\n\n Check whether a number is prime or not:\n";
cout << "--------------------------------------------\n";
cout << " Input a number to check prime or not: ";
cin>> num1;
for (int a = 1; a <= num1; a++)
{
if (num1 % a == 0)
{
ctr++;
}
}
if (ctr == 2)
{
cout << " The entered number is a prime number. \n";
}
else {
cout << " The number you entered is not a prime number. \n";
}
}
Computer Skills & Programming II Dr. Shayma Akram

Write a program in C++ to find the factorial of a number.

#include <iostream>
using namespace std;
int main()
{
int num1,factorial=1;
cout << "\n\n Find the factorial of a number:\n";
cout << "------------------------------------\n";
cout << " Input a number to find the factorial: ";
cin>> num1;
for(int a=1;a<=num1;a++)
{
factorial=factorial*a;
}
cout<<" The factorial of the given number is: "<<factorial<<endl;
return 0;
}

Sample Output:

Find the factorial of a number:


------------------------------------
Input a number to find the factorial: 5
The factorial of the given number is: 120
Computer Skills & Programming II Dr. Shayma Akram

Write a C++ program to check whether a given number is a power of two or not.

#include <iostream>
#include <cmath>
using namespace std;
string Powers_of_Two(int n) {

for (int x = 0; x < INT_MAX; x++)


{
if (pow(2, x) == n)
{
return "True";
}
else if (pow(2, x) > n)
{
break;
}
}

return "False";
}
int main() {
cout << "Is 8 is power of 2: " << Powers_of_Two(8) << endl;
cout << "Is 256 is power of 2: " << Powers_of_Two(256) << endl;
cout << "Is 124 is power of 2: " << Powers_of_Two(124) << endl;
return 0;
}
Computer Skills & Programming II Dr. Shayma Akram

Sample Output:

Is 8 is power of 2: True
Is 256 is power of 2: True
Is 124 is power of 2: False

Write a C++ program to calculate x raised to the power n (xn).

#include <iostream>
using namespace std;

double powxn(double x, int n) {


if (n < 0){
x = 1 / x;
n = -n;
}

double result = 1;
for (auto i = 0; i < n; ++i)
{
result = result * x;
}

return result;
}
Computer Skills & Programming II Dr. Shayma Akram

int main(void)
{
double x = 7.0;
int n = 2;
cout << "\n" << x << "^" << n << " = " << powxn(x, n) << endl;
x = 3;
n = 9;
cout << "\n" << x << "^" << n << " = " << powxn(x, n) << endl;
x = 6.2;
n = 3;
cout << "\n" << x << "^" << n << " = " << powxn(x, n) << endl;
return 0;
}

Sample Output:

7^2 = 49

3^9 = 19683

6.2^3 = 238.328
Computer Skills & Programming II Dr. Shayma Akram

Recursion
Write a program to calculate the sum of numbers from 1 to n using recursion.

#include <iostream>
using namespace std;

int sumOfRange(int);

int main()
{
int n1;
int sum;
cout << ("\n\n Recursion : calculate the sum of numbers from 1 to n :") << endl;
cout << ("-----------------------------------------------------------") << endl;

cout << " Input the last number of the range starting from 1 : ";
cin >> n1;
sum = sumOfRange(n1);
cout << "The sum of numbers from 1 to " << n1 << endl;
cout << sum;

return (0);
}

int sumOfRange(int n1)


{
int res;
if (n1 == 1)
{
return (1);
}
Computer Skills & Programming II Dr. Shayma Akram

else
{
res = n1 + sumOfRange(n1 - 1); //calling the function sumOfRange itself
}
return (res);
}

Sample Output:
Recursion : calculate the sum of numbers from 1 to n :
-----------------------------------------------------------
Input the last number of the range starting from 1 : 5

The sum of numbers from 1 to 5 : 15

Write a program to print the array elements using recursion.

#include <iostream>
using namespace std;

#define MAX 10

void ArrayElement(int arr1[], int st, int l);

int main()
{
int arr1[MAX];
int n, i;
cout << (" Recursion : Print the array elements :");

cout << (" Input the number of elements to be stored in the array :");
cin >> n;
Computer Skills & Programming II Dr. Shayma Akram

cout << " Input elements of the array : " << endl;
for (i = 0; i < n; i++)
{
cout << " element - : " << i + 1 << ") ";
cin >> arr1[i];
}

cout << (" The elements in the array are : ");


ArrayElement(arr1, 0, n); //call the function ArrayElement
return 0;
}

void ArrayElement(int arr1[], int st, int l)


{
if (st >= l)
return;

//Prints the current array element


cout << arr1[st] << " ";

/* Recursively call ArrayElement to print next element in the array */


ArrayElement(arr1, st + 1, l); //calling the function ArrayElement itself
}

Sample Output:
Recursion : Print the array elements : Input the number of elements to be
stored in the array :3
Input elements of the array :
element - : 1) 4
element - : 2) 5
element - : 3) 6

You might also like