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

Uzma Nisar - 033

Uzma Nisar submitted assignment 1 which includes 10 programming problems and their solutions in C++. The problems cover topics like checking if numbers are amicable, finding consecutive repeating digits, rolling a die simulator, counting words in a paragraph, reversing an array, finding a missing element in an array, calculating the product of matrix triangles, data type sizes, swapping variables, and comparing numbers without if statements. The machine used is 64-bit.

Uploaded by

uzma nisar
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)
48 views

Uzma Nisar - 033

Uzma Nisar submitted assignment 1 which includes 10 programming problems and their solutions in C++. The problems cover topics like checking if numbers are amicable, finding consecutive repeating digits, rolling a die simulator, counting words in a paragraph, reversing an array, finding a missing element in an array, calculating the product of matrix triangles, data type sizes, swapping variables, and comparing numbers without if statements. The machine used is 64-bit.

Uploaded by

uzma nisar
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/ 51

ASSIGNMENT#1

Uzma Nisar | CIIT/sp21-Rcs-033/ISB


MS Computer Science (Fall-21) | Theory of Programming Languages
Dr. Manzoor Illahi Tamimy
18-03-2021
Department of Computer Science – Comsats University Islamabad

1. Amicable numbers are found in pairs. A given pair of numbers is Amicable if the sum of the
proper divisors (not including itself) of one number is equal to the other number and vice – versa.

For example, 220 & 284 are amicable numbers first we find the proper divisors of 220:

220:1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110

1+ 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = 284

Now, 284: 1, 2, 4, 71, 142

1 + 2 + 4 + 71 + 142 = 220

Write a C program to check that the input pair of numbers is amicable

#include <iostream>
using namespace std;
int main(){
int N1, N2;
int sum1 = 0;
int sum2 = 0;
cout<<"please enter the first number: ";
cin>>N1;
cout<<"please enter the second number: ";
cin>>N2;
for(int i = 1; i <= N1/2; i++){
if(N1 % i == 0){
sum1 = sum1+i;
}
}
for(int i = 1; i <= N2/2; i++){
if(N2 % i == 0){
sum2 = sum2+i;
}
}
if(N1 == sum2 && sum1 == N2){
cout<<"This is Amicable";
}
else

Assignment#1 2
Department of Computer Science – Comsats University Islamabad

cout<<"Not Amicable";
}
return 0;
}

2. Write a C program to check if a number has three consecutive 5s. If yes, print YES, else print
NO.
Example:
Number: 1353554
Result: NO
Number: 345559
Result: YES
#include <iostream>
using namespace std;
int main(){

Assignment#1 3
Department of Computer Science – Comsats University Islamabad

int input[10], count, i, num;


cout << "Enter Number of Elements in Array\n";
cin >> count;
cout << "Enter " << count << " numbers \n";
for(i = 0; i < count; i++){
cin >> input[i];
}
cout << "Enter a number to serach in Array\n";
cin >> num;
for(i = 0; i < count; i++){
if(input[i] == num && input[i+1] == num && input[i+2] == num){
cout << "Element found 3 times consecutively from index " << i <<" TO "<<i+2;
break;
}
}
if(i == count){
cout << "Element Not Present in Input Array\n";
}
return 0;
}

Assignment#1 4
Department of Computer Science – Comsats University Islamabad

3. Write a C++ program to roll a six-sided die 100 times.


#include <iostream>
#include <string>
#include<ctime>
#include <stdlib.h>
using namespace std;
int main()
{
srand((unsigned) time (NULL));
int arr[6]= {0,0,0,0,0,0};
for(int i=0;i<100;i++)
{

arr[rand()%6] +=1;

}
for(int i=0;i<6;i++)
{
cout<<(i+1)<<" rolled "<<arr[i]<<" times "<<endl;
}
return 0;
}

Assignment#1 5
Department of Computer Science – Comsats University Islamabad

4. Write a c program which reads a paragraph from user and count number of words in a
paragraph.
#include <iostream>
using namespace std;
int main()
{
int count=0;
cout<<"Please write a paragraph which you want to count"<<"\n";
string paragraph;
getline(cin,paragraph);
cout<<paragraph<<"\n";
cout<<"Length of the paragraph is: " <<paragraph.length();
return 0;
}

5. Write a c program which reads values of array and print array in reverse order.
include <iostream>
using namespace std;
int main(){
int array[8]={1,5,4,8,9,2,3,7};
int i;
cout<<"The array in the original order is: ";
for(i=0;i<8;i++){

cout<<array[i];

Assignment#1 6
Department of Computer Science – Comsats University Islamabad

cout<<"\n";
cout<<"The Array in the Reverse order is:";
for(i=7;i>=0;i--){

cout<<array[i];
}
return 0;
}.

6. Write a C program to find the missing character in an array.


#include <iostream>
using namespace std;
int main()
{
int n, i, j, c, t, b;

cout<<"Enter size of array : ";


cin>>n;
int array[n - 1]; /* array size-1 */

Assignment#1 7
Department of Computer Science – Comsats University Islamabad

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


for (i = 0; i < n - 1; i++)
cin>>array[i];
b = array[0];
for (i = 1; i < n - 1; i++)
b = b ^ array[i];
for (i = 2, c = 1; i <= n; i++)
c = c ^ i;
c = c ^ b;
cout<<"Missing element is : \n" << c <<endl;
return 0;
}

7. Write a program in C to find the sumof (productsof) lower and upper triangular elements
of a matrix.
#include <iostream>
using namespace std;
int main()
{
int num[20][20], i,j,n,matrixproduct=1, matrixproduct1=1;
cout << "Enter number of rows and columns of Matrix: ";

Assignment#1 8
Department of Computer Science – Comsats University Islamabad

cin >> n;
cout << "\nEnter Matrix Elements! ";
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
cout << "\nEnter elements in matrix: ", i,j;
cin >> num[i][j];
}
}
cout << "Matrix is as follows \n";

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


{
for(j=0; j<n; j++)
{
cout << num[i][j] << " ";
}
cout << endl;}cout << endl;
// product of upper triangular
for(i=0; i<n; i++){
for(j=0; j<n; j++){
if(i<j)
{
matrixproduct*=num[i][j];
}}}
cout << "product of upper triangular matrix " << matrixproduct;
cout << endl;
// product of lower triangular
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(i>j)

Assignment#1 9
Department of Computer Science – Comsats University Islamabad

{
matrixproduct1*=num[i][j];
}
}
}
cout << "product of lower triangular matrix " << matrixproduct1 << endl;
int res= matrixproduct+matrixproduct1;
cout << "Sum of upper and lower triangular matrices are : "<< res;
return 0;
}

Assignment#1 10
Department of Computer Science – Comsats University Islamabad

8. Determine the size, minimum and maximum value following data types. Please specify if
your machine is 32 bit or 64 bits in the answer.

a) Char
b) Short
c) Unsigned char
d) Unsigned int
e) Int
f) Unsigned long
g) Float
#include <iostream>
using namespace std;
int main()
{
int i,value;
char c;
float f;
short s;
unsigned int ui;
unsigned char uch;
unsigned long unl;
value = 9816253748586;
cout<<"size of int" <<sizeof(i)<<endl;
cout<<"size of char" <<sizeof(c)<<endl;
cout<<"size of float" <<sizeof(f)<<endl;
cout<<"size of short" <<sizeof(s)<<endl;
cout<<"size of unsigned int" <<sizeof(ui)<<endl;
cout<<"size of unsigned char" <<sizeof(uch)<<endl;
cout<<"size of unsigned long" <<sizeof(unl)<<endl;

cout<<value;
return 0;
}

Assignment#1 11
Department of Computer Science – Comsats University Islamabad

9. Swap two numbers in C using addition, subtraction, and assignment operators:


Logic.

#include <iostream>
using namespace std;
int main()
{
int x = 10, y = 5;
x = x + y;
y = x - y;
x = x - y;
cout << "After Swapping: x =" << x << ", y=" << y;
return 0;
}

Assignment#1 12
Department of Computer Science – Comsats University Islamabad

10. Write a C program to compare two numbers without IF statement.

#include <iostream>

using namespace std;

int main()
{

int firstNo;
int secondNo;
int compare;

cout<<"Enter first number : ";


cin>>firstNo;
cout<<"Enter second number : ";
cin>>secondNo;
compare=(firstNo>secondNo)?firstNo:secondNo;
cout<<"greater value is "<<compare<<endl;
compare=(firstNo<secondNo)?firstNo:secondNo;
cout<<"smaller value is "<<compare<<endl;
return 0;

Assignment#1 13
Department of Computer Science – Comsats University Islamabad

11. Write a C program to find all roots of a quadratic equation.

#include <iostream>
#include <cmath>
using namespace std;

int main() {

float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;


cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
discriminant = b*b - 4*a*c; //formula of quadratic euqation

if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;

Assignment#1 14
Department of Computer Science – Comsats University Islamabad

else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = -b/(2*a);
cout << "x1 = x2 =" << x1 << endl;
}

else {
realPart = -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}

return 0;
}

Assignment#1 15
Department of Computer Science – Comsats University Islamabad

12. Write a C program to check whether a character is an alphabet, digit, or special character.

#include <iostream>
int isAlphabet(char l)
{
int ascii=l;
if(( l>=65 && l<=90) || (l>=97 && l<=122))
{

return 1;

}
return 0;
}
int isDigit(char l)
{
int ascii=l;
if((l>=48 && l<=57))
{

return 1;

}
return 0;
}
int isSpecialChar(char l)
{

if(isDigit(l) || isAlphabet(l))
{
return 0;

Assignment#1 16
Department of Computer Science – Comsats University Islamabad

return 1;
}
int main()
{
char letter;
printf("Enter a Character :");
scanf("%c",&letter);
printf("is Alphabet:%d\n",isAlphabet(letter));
printf("is Digit:%d\n",isDigit(letter));
printf("is Special Character:%d\n",isSpecialChar(letter));
return 0; }

Assignment#1 17
Department of Computer Science – Comsats University Islamabad

13. Display Armstrong number between intervals

#include <iostream>
using namespace std;

int main()
{
int number,i,digit,sum=0;
cout<<"Arm Strong Number from 1 to 999 are :";
for(i=1;i<1000;i++)
{ number=i;
while(number>0)
{
digit = number%10;
sum+=digit*digit*digit;
number=number/10; }
if(sum==i)
cout<<"\n"<<i;
sum=0;
}
}

Assignment#1 18
Department of Computer Science – Comsats University Islamabad

14. Write a C program to find the eligibility of admission for a professional course based on
the following criteria:

Eligibility Criteria : Marks in Maths >=65 and Marks in Phy >=55 and Marks in
Chem>=50 and Total in all three subject >=190 or Total in Maths and Physics >=140 -------
------------------------------ Input the marks obtained in Physics :65 Input the marks obtained
in Chemistry :51 Input the marks obtained in Mathematics :72 Total marks of Maths,
Physics and Chemistry : 188 Total marks of Maths and Physics : 137 The candidate is not
eligible.
#include <iostream>
using namespace std;
int main()
{
int maths,physics,chemistry;
cout<<"please enter the marks in Maths :";
cin>>maths;
cout<<"please enter the marks in Physics :";
cin>>physics;
cout<<"please enter the marks in Chemistry :";
cin>>chemistry;

if(maths>=65 && physics>=55 && chemistry>=50 && (maths+physics+chemistry)>=180)


cout<<"Congrats! you are eligible for admission";
else if (maths+physics>=140)
cout<<"Congrats! you are eligible for admission";
else
cout<<"Oops sorry you are not eligible for admission";
return 0;
}

Assignment#1 19
Department of Computer Science – Comsats University Islamabad

15. Using precedence rules, evaluate the following expressions and determine the value of the
variables (without running the code). Also rewrite them using parenthesis to make the order
explicit.
a) Assume (x=0xFF33,MASK=0xFF00).Expression: c=x & MASK ==0;

The operator precedence is ‘= =’ >’&’>’=’. Thus, the expression is equivalent to c= (x &


(MASK == 0)). Therefore x= 0xFF33, c=0.

b) Assume (x=10,y=2,z=2;).Expression: z=y=x++ + ++y∗2;

The operator precedence is ‘= =’>’&’>’=’.thus, the expression is equivalent to z= (x++) +


((++y)*2). Therefore x = 11, y=3, z=10+3*2=16

c) Assume (x=10,y=4,z=1;).Expression: y>>= x&0x2 && z

The operator precedence is ‘&’>’&&’>’>>=’.thus the expression is equivalent to y>>=(x&


0x2) && z. therefore x=10,y=2,z=1.

16. Determine the hierarchy of operations and evaluate the following expression:

𝟑+𝟒𝒙 𝟏𝟎(𝒚−𝟓)(𝒂+𝒃+𝒄) 𝟒 𝟗+𝒙


a) − + 𝟗( + )
𝟓 𝒙 𝒙 𝒚

Assignment#1 20
Department of Computer Science – Comsats University Islamabad

𝒇𝒊𝒓𝒔𝒕 𝒘𝒆 𝒆𝒗𝒂𝒍𝒖𝒂𝒕𝒆𝒅
(3 + 4 * x)/5 – 10 * (y - 5)*(a + b + c)/x + 9 *(4 / x + (9 + x)/y)
• Operators contained within pairs of parentheses are evaluated first.
• Parentheses can be nested, in which case the expression in the inner parentheses is evaluated
first.
• Multiplication, division, and remainder operators are applied next.
Order of operation is applied from left to right.
Addition and subtraction are applied last.

b) 𝟓.5* ((𝒓 + 𝟐. 𝟓)𝟐.𝟓+𝒕 )

5.5 * Math.pow(r + 2.5, 2.5 + t)

𝟒 𝟑+𝒅(𝟐+𝒂)
c) − 𝟗(𝒂 + 𝒃𝒄) +
𝟑(𝒕+𝟑𝟒) 𝒂+𝒃𝒅

4.0 / (3 * (t+ 34)) - 9.0 * (a + b * c) + (3 + d * (2 + a))/ (a + b * d)

17. Write a program in C to merge one sorted array into another sorted array.
#include<iostream>
using namespace std;
int main(){
int arr[5] = {0 , 1, 6, 6,9};
int arr1[6] = {2, 3,7,9,11,12};
int sorted[11];
int i=0,j=0;
cout<<"My sorted Array is: "<<endl;
for(int k = 0; k<11; k++){

if(arr[i] < arr1[j] && i<5){


sorted[k] = arr[i];
i++;
}else{
sorted[k] = arr1[j];

Assignment#1 21
Department of Computer Science – Comsats University Islamabad

j++;
}
}

for(int k = 0; k<11; k++){


cout<<sorted[k];}
return 0;
}

18. Write a C program to find and remove duplicate elements of array


#include<iostream>
using namespace std;
int main(){
int arr[16] = {1 , 2, 4, 6,8,2,2,3,4,6,7,8,9,1,12};
for(int i = 0; i<16; i++){
int Val = arr[i];
for(int j=i+1;j<16;j++){

Assignment#1 22
Department of Computer Science – Comsats University Islamabad

if(Val == arr[j]){
arr[j] = 0;
}
}
}
for(int k = 0;k<16;k++){
cout<<arr[k];
}
return 0;
}

19. Write a C program, that reads list of n integer and print sum of product of consecutive
numbers.
#include <iostream>

using namespace std;

int main()
{
int n,sum=0;

Assignment#1 23
Department of Computer Science – Comsats University Islamabad

cout<<"Enter size of the list which you want to read:";


cin>>n;
int list[n];
cout<<"Enter Numbers in Array: "<<endl;
for(int i=0;i<n;i++)
{

cout<<"LIST OF ARRAY IS [" <<i<<"] " ;


cin>>list[i];
}
for(int i=0;i<n-1;i++)
{

sum+=(list[i] * list [i+1]);

}
cout<<"Sum of Product of value is : " <<sum;
return 0;
}

Assignment#1 24
Department of Computer Science – Comsats University Islamabad

20. Write a C program to find the sum of First, Third and Last element of array and then replace
the zero index with sum. Then find the product of second and fourth element and place it at index
1 of array. Then sort the values of array in descending order to print array. Also show stack trace
for this program.
#include<iostream>
using namespace std;
int main(){
int n, m, sum;
cout<<"please enter the size of array you want to make: ";
cin>>n;
int array[n];
cout<<"array initialization: "<<endl;
for(int i = 0; i <n; i++){
cout<<"please enter the "<<m<<" value: ";
cin>>array[i];
m++;
}
sum = array[0]+ array[2]+ array[n-1];
array[0] = sum;
array[1] = array[1]*array[3];
int temp;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(array[i]<array[j])
{
temp =array[i];
array[i]=array[j];
array[j]=temp;
}

Assignment#1 25
Department of Computer Science – Comsats University Islamabad

}
}

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


cout<<array[i]<<" ";
}return 0;}

21. Write a program in C++ to check overflow/underflow during various arithmetical operation

#include <iostream>
using namespace std;

int main()
{
cout << "\n\n Check overflow/underflow during various arithmetical operation :\n";
cout << " Range of int is [-2147483648, 2147483647]"<< endl;
cout << "---------------------------------------------------------------------\n";
int n1 = 2147483647; // maximum range of integer
cout <<" Overflow the integer range and set in minimum range : " << n1 + 1 << endl;
cout <<" Increasing from its minimum range : " << n1 + 2 << endl;

Assignment#1 26
Department of Computer Science – Comsats University Islamabad

cout <<" Product is :" << n1 * n1 << endl;

int n2 = -2147483648; // minimum range of integer


cout <<" Underflow the range and set in maximum range : " << n2 - 1 << endl;
cout <<" Decreasing from its maximum range : " << n2 - 2 << endl;
cout <<" Product is : " << n2 * n2 << endl;
cout << endl;
return 0;
}

22. Write a program in C to find the pivot element of a sorted and rotated array using binary
search
#include <stdio.h>
int findPivotElem(int *arr1, int left_elem, int right_elem)
{
if (right_elem < left_elem)
return -1;
if (right_elem == left_elem)
return left_elem;
int mid_elem = (left_elem + right_elem)/2;
if (mid_elem < right_elem && arr1[mid_elem] > arr1[mid_elem + 1])

Assignment#1 27
Department of Computer Science – Comsats University Islamabad

return mid_elem;
if (mid_elem > left_elem && arr1[mid_elem] < arr1[mid_elem - 1])
return mid_elem-1;
if (arr1[left_elem] >= arr1[mid_elem])
{
return findPivotElem(arr1, left_elem, mid_elem-1);
} else
{
return findPivotElem(arr1, mid_elem + 1, right_elem);
}
}
int main()
{
int i;
int arr1[] = {14, 23, 7, 9, 3, 6, 18, 22, 16, 36};
int ctr = sizeof(arr1)/sizeof(arr1[0]);
printf("The given array is : ");
for(i = 0; i < ctr; i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
printf("The Pivot Element in the array is : %d \n", arr1[findPivotElem(arr1, 0, ctr-1) + 1]);
return 0;
}

Assignment#1 28
Department of Computer Science – Comsats University Islamabad

23. Write a C program which read an upper-case character array from user and replace each
character with lower case ascii value of same character.
#include <iostream>
#include <math.h>
using namespace std;
void populateArray(char arr[], int arrSize)
{
for(int i=0; i<arrSize;i++)
{
cin>>arr[i];
}
}
void displayArray(char arr[], int arrSize)
{
for(int i=0; i<arrSize;i++)
{
cout<<arr[i]<<" ";
}
}

Assignment#1 29
Department of Computer Science – Comsats University Islamabad

void lowerCase(char arr[], int arrSize)


{
for (int i=0; i<arrSize; i++)
{
arr[i]=arr[i]+32;
}
}

void multiplyOp(int arr[], int arrSize)


{
arr[1] =arr[1]*arr[3];
}
void sortArray(int arr[], int arrSize)
{
for(int i=0; i<arrSize; i++)
{
for(int j=0; j<arrSize; j++)
if(arr[i]>arr[j])
{
arr[i]=(arr[j]+arr[i])-(arr[j]=arr[i]);
}
}
}
int main()
{
int arrSize;
cout<<"Enter size of array \n";
cin>>arrSize;
char arr[arrSize];
cout<<"Enter Capital Characters \n";
populateArray(arr, arrSize);

Assignment#1 30
Department of Computer Science – Comsats University Islamabad

cout<<"input Array :";


displayArray(arr, arrSize);
cout<<"\nLower case array : ";
lowerCase(arr, arrSize);
displayArray(arr, arrSize);

return 0;
}

24. Write a C program that prints pyramid and half pyramid.

#include <iostream>
using namespace std;
int main()
{
int n;

Assignment#1 31
Department of Computer Science – Comsats University Islamabad

cout << "Enter number of lines: ";


cin >> n;

cout << endl;

// loop for line number of lines


for(int i = 1; i <= n; i++)
{
// loop to print leading spaces in each line
for(int space = 0; space <= n - i; space++)
{
cout << " ";
}

// loop to print *
for(int j = 1; j <= i * 2 - 1; j++)
{
cout << " * ";
}

cout << endl;


}

cout << endl;


cout << endl;

// half pyramid
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= i; j++)

Assignment#1 32
Department of Computer Science – Comsats University Islamabad

{
cout << " * ";
}
cout << endl;
}

return 0;
}

25. Write a C program to find whether one array is subset of other array or both arrays are
mirror image of one another.
#include <bits/stdc++.h>
using namespace std;
bool isSubset(int arr1[], int arr2[],
int m, int n)
{
int i = 0;
int j = 0;
for (i = 0; i < n; i++) {

Assignment#1 33
Department of Computer Science – Comsats University Islamabad

for (j = 0; j < m; j++) {


if (arr2[i] == arr1[j])
break;
}
if (j == m)
return 0;
}

return 1;
}

int main()
{
int n,m;
cout<<"\nEnter the size of Array 1 : "<<endl;
cin>>m;
cout<<"\nEnter the size of Array 2 : "<<endl;
cin>>n;
int arr1[m],arr2[n];
int i;
cout<<"\nInput the Array 1 elements : "<<endl;
for(i = 0; i < m; i++)
{
cin>>arr1[i];
}
cout<<"\nInput the Array 2 elements : "<<endl;
for(i = 0;i<n;i++)
{
cin>>arr2[i];
}

Assignment#1 34
Department of Computer Science – Comsats University Islamabad

m = sizeof(arr1) / sizeof(arr1[0]);
n = sizeof(arr2) / sizeof(arr2[0]);

if (isSubset(arr1, arr2, m, n))


printf("arr2[] is subset of arr1[] ");
else
printf("arr2[] is not a subset of arr1[]");

getchar();
return 0;
}

26. Write a program in C to find the ceil & floor of any element in an array.

#include <iostream>
using namespace std;
int FindCeil(int arr1[], int n, int x)
{
int low = 0, high = n - 1, mid;

Assignment#1 35
Department of Computer Science – Comsats University Islamabad

int ceil = -1;


while (low <= high)
{
mid = (low + high) / 2;
if (arr1[mid] == x)
return arr1[mid];
else if (x < arr1[mid])
{
ceil = arr1[mid];
high = mid - 1;
}
else
low = mid + 1;
}
return ceil;
}

int FindFloor(int arr1[], int n, int x)


{
int low = 0, high = n - 1, mid;
int floor = -1;
while (low <= high)
{
mid = (low + high) / 2;
if (arr1[mid] == x)
return arr1[mid];
else if (x < arr1[mid])
high = mid - 1;
else
{
floor = arr1[mid];

Assignment#1 36
Department of Computer Science – Comsats University Islamabad

low = mid + 1;
}
} return floor;
}
int main()
{
int n;
cout<< "\nEnter the size of Array : " << endl;
cin>>n;
int arr1[n],i;
cout<<"\nInput the Array 1 elements : "<< endl;
for(i = 0; i < n; i++)
{
cin>>arr1[i];
}
int ctr = sizeof(arr1) / sizeof(arr1[0]);
cout<<"The given array is : "<<endl;
for(i = 0; i < ctr; i++)
{
cout<<arr1[i];
}
cout<<"\n";

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


{
cout<<"\n\n number: "<< i;
cout<<"\nceiling is: " <<FindCeil(arr1, ctr, i);
cout<<"\nfloor is: " << FindFloor(arr1, ctr, i);
}
return 0;
}

Assignment#1 37
Department of Computer Science – Comsats University Islamabad

27. Write a C program to reverse the sum of consecutive numbers


#include <iostream>
using namespace std;
int reversDigits(int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
int main() {
int num, sum = 0;

cout << "Enter a positive integer: ";

Assignment#1 38
Department of Computer Science – Comsats University Islamabad

cin >> num;

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


sum += i;
}
cout << "Sum = " << sum;
cout << "\nReverse the sum of: " << reversDigits(sum);
}

28. Write a C++ program to isolate rightmost zero bit of a number.

Output:
original number before isolating rightmost 0 bit: 11
new number after isolating rightmost 0 bit: 4

#include <iostream>
using namespace std;

int isolate_rightmost_zerobit(int n)
{
int mask1=n+1; //add 1 to original number

Assignment#1 39
Department of Computer Science – Comsats University Islamabad

int mask2=~(n); // bitwise complement of original number


return (mask1&mask2); // new number after isolating rightmost 0 bit
}

//driver program to check the code

int main()
{
int num;

cout<<"Enter number: ";


cin>>num;
cout<<"original number before isolating rightmost 0 bit: "<<num<<endl;

int new_number= isolate_rightmost_zerobit(num);

cout<<"new number after isolating rightmost 0 bit: "<<new_number<<endl;

return 0;
}

Assignment#1 40
Department of Computer Science – Comsats University Islamabad

29. Write a program to determine color of chess square.

#include<cctype>

using namespace std;

int main()

char string[10], x;

cout << "Enter the coordinates of the square, \

\n the first coordinate A to H and second coordinate 1 to 8: ";

cin.getline(string, 10);

x = string[0];

x = tolower(x);

string[0] = x;

if (string[0] == 'a' || string[0] == 'c' || string[0] == 'e' || string[0] == 'g')

if (string[1] == '1' || string[1] == '3' || string[1] == '5' || string[1] == '7')

cout << "Black square";

else

cout << "White square";

else

if (string[1] == '1' || string[1] == '3' || string[1] == '5' || string[1] == '7')

Assignment#1 41
Department of Computer Science – Comsats University Islamabad

cout << "white square";

else

cout << "Black square";

return 0;

30. Write a program in C which is a Menu-Driven Program to compute the area of the various
geometrical shape.

#include<iostream>

using namespace std;

int main()

Assignment#1 42
Department of Computer Science – Comsats University Islamabad

int choice,r,l,w,b,h;

float area;

cout<<"Input 1 for area of Circle\n";

cout<<"Input 2 for area of Triangle\n";

cout<<"Input 3 for area of Rectangle\n";

cout<<"Input your choice : ";

cin>>choice;

switch(choice)

case 1:

cout<<"\nInput radius of the circle : ";

cin>>r;

area=3.14*r*r;

break;

case 2:

cout<<"\nInput the base and hight of the triangle :";

cin>>b;

cin>>h;

area=.5*b*h;

break;

case 3:

Assignment#1 43
Department of Computer Science – Comsats University Islamabad

cout<<"\nInput length and width of the rectangle : ";

cin>>l;

cin>>w;

area=l*w;

break;

cout<<"The area is : \n"<< area <<endl;

31. Write a program in C which is a Menu-Driven Program to do all arithmetic operations. Make
sure there is different function for each individual arithmetic operation. Values for arithmetic
operations should be read from the user. Program will only exit from the last available choice of
menu available in your program (exit). Your program must contain data validation checks on
every input from the keyboard.

Assignment#1 44
Department of Computer Science – Comsats University Islamabad

#include <iostream>
using namespace std;

void showChoices();
float add(float, float);
float subtract(float, float);
float multiply(float, float);
float divide(float, float);

int main()
{
float x, y;
int choice;
do
{
showChoices();
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Sum " << add(x,y) <<endl;
break;
case 2:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Difference " << subtract(x,y) <<endl;
break;
case 3:

Assignment#1 45
Department of Computer Science – Comsats University Islamabad

cout << "Enter two numbers: ";


cin >> x >> y;
cout << "Product " << multiply(x,y) <<endl;
break;
case 4:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Quotient " << divide(x,y) <<endl;
break;
case 5:
break;
default:
cout << "Invalid input" << endl;
}
}while (choice != 5);

return 0;
}

void showChoices()
{
cout << "MENU" << endl;
cout << "1: Add " << endl;
cout << "2: Subtract" << endl;
cout << "3: Multiply " << endl;
cout << "4: Divide " << endl;
cout << "5: Exit " << endl;
cout << "Enter your choice :";
}

float add(float a, float b)

Assignment#1 46
Department of Computer Science – Comsats University Islamabad

{
return a + b;
}

float subtract(float a, float b)


{
return a - b;
}

float multiply(float a, float b)


{
return a * b;
}

float divide(float a, float b)


{
return a / b;
}

Assignment#1 47
Department of Computer Science – Comsats University Islamabad

32. Fix the errors and write output of following statements:

a) printf ("TRUE %nOR %nFALSE");


b) printf ("10 %% 4 = 2");
c) println("Hello! \f How are you all? ");
d) println("\\- this is a backslash. ");
e) println("Good Morning\t Geeks! ");
f) println("Hi Learners, welcome to \"CUI\".");
g) printf( "%s\n%s\n%s\n", "*", "***", "*****" );
h) printf(“%+d”, 2502);
i) printf ("|%-20.4f|", 1234.1234);
j) printf ("10 %% 4 = 2");
k) printf ( "%E", 123.1234);
l) e= 123.1234;
println("UpperCase Scientific Notation" + " using %E: " + e);
m) Int x=1245;
printf("Number: %o\n", x);
n) Long n= 461012;

printf ("%+8d%n", n);

Assignment#1 48
Department of Computer Science – Comsats University Islamabad

o) Double pi= 3.142;


printf ("%.3f%n", pi);
printf ("%10.3f%n", pi);
p) int x=6, y =12, z;

z=x*y;
s=x+y;
println("Product= " + z + “and” +" Sum= "+ s +".");
q) Int i=88;
printf ("%010d", i);
println ("value in 10 digits: + i);
r) println (%.10s", "12345678901234567890");
s) String firstname= Sara;
String lastname= Khan;
println("First Name: " + firstname + " Last Name: "+ lastname +".");
println ("First Name: %s", firstname);
println ("Last Name: %s", lastname);
t) printf("%s\n%s\n”, “Welcome to” , “ C programming”);
u) int a=6, b=12, c=12;
sum= a+b+c;
printf(“Sum is %d\n”, sum);
v) int num1=5;
int num2=6;
if (num1 !=num2)
printf(“%d != %d \n”, num1, num2 );
w) char str[] = "cprogrammingexercise";

printf("%20s\n", str);
printf("%-20s\n", str);
printf("%20.5s\n", str);
printf("%-20.5s\n", str);

x) int a = 0;

scanf("%d", &a); // input is 45


printf("%d\n", a);

y) int a = 0;
scanf("%i", &a);
printf("%d\n", a);
scanf("%i", &a);
printf("%d\n", a);

Assignment#1 49
Department of Computer Science – Comsats University Islamabad

z) char name[20] = "Bob";


int age = 21;
printf("Hello %s, you are %d years old\n", name, age);

a) True
b) 2
c) Hello! How are you all?
d) \- this is a backslash.
e) Good Morning Geeks!
f) Hi Learners, welcome to "CUI".
g) *
*
***
h) 2504
i) |1234.1234 |
j) 10 % 4 = 2
k) 1.231234E+002
l) UpperCase Scientific Notation using 1.231234E+002:
m) Number: 2335
n) +461012
o) 3.142 3.142
p) Product= 72 and Sum =18.
q) value in 10 digits: 0000000088
r) 1234567890
s) First Name: Sara Last Name: Khan. First Name: Sara Last Name: Khan
t) Welcome to
C programming
u) Sum is 30
v) 5 != 6
w) cprogrammingexercise
cprogrammingexercise
cprog
cprog
x) 45
y) if I input value 4 in a then output is also 4
z) Hello Bob, you are 21 years old

Assignment#1 50
Department of Computer Science – Comsats University Islamabad

Assignment#1 51

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy