WameedMUCLecture 2021 92910486
WameedMUCLecture 2021 92910486
Shayma Akram
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;
return 0;
}
Example
Input
#include<iostream>
using namespace std;
if (arr_size < 3)
{
cout << "Invalid Input";
}
{
third = second;
second = first;
first = arr[i];
}
else if (arr[i] > second)
{
third = second;
second = 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
Write a C++ program to find the most occurring element in an array of integers.
#include<iostream>
using namespace std;
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;
prev_element = curr_element;
}
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:
// 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;
}
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:
#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
#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:
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) {
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
#include <iostream>
using namespace std;
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);
}
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
#include <iostream>
using namespace std;
#define MAX 10
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];
}
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