0% found this document useful (0 votes)
44 views13 pages

Ajit Pant MSM Enrollment No 18312004 Batch S2

The document contains solutions to 7 programming assignments involving functions in C++. Assignment 1 defines a function to check if a number is even or odd. Assignment 2 separates even and odd elements of an array into separate arrays. Assignment 3 implements bubble sort to sort an array in descending order. Assignment 4 defines functions to read, display, add and multiply 2D matrices. Assignment 5 rotates a 2D square matrix 90 degrees clockwise. Assignment 6 demonstrates functions for character input/output like getchar(), putchar() etc. Assignment 7 defines functions operating on C++ strings.

Uploaded by

ajitpant
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)
44 views13 pages

Ajit Pant MSM Enrollment No 18312004 Batch S2

The document contains solutions to 7 programming assignments involving functions in C++. Assignment 1 defines a function to check if a number is even or odd. Assignment 2 separates even and odd elements of an array into separate arrays. Assignment 3 implements bubble sort to sort an array in descending order. Assignment 4 defines functions to read, display, add and multiply 2D matrices. Assignment 5 rotates a 2D square matrix 90 degrees clockwise. Assignment 6 demonstrates functions for character input/output like getchar(), putchar() etc. Assignment 7 defines functions operating on C++ strings.

Uploaded by

ajitpant
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
You are on page 1/ 13

AJIT PANT MSM ENROLLMENT NO 18312004 BATCH S2

ASSIGNMENT 4 (MAN 103)

1. Write a Boolean function bool iseven(int n) to check whether a no. is Even or


Odd. Write main to test the function.
Answer
#include<iostream>
using namespace std;
bool iseven(int n)
{
if(n%2==0)
return 1;
return 0;
}
int main()
{
cout<<”5 is “<<iseven(5)<<endl;
return 0;
}
Output:
5 is 0
2. Write a C++ program to separate even and odd elements of a given array.
Display the given array, array of even numbers and array of odd numbers.
Answer:
#include<iostream>
#define SIZE 100
using namespace std;
AJIT PANT MSM ENROLLMENT NO 18312004 BATCH S2

void swap(int &a, int &b)


{
int temp = a;
a = b;
b = temp;
}

int main()
{
int arr[SIZE] = {12, 34, 45, 9, 8, 90, 3};
int n= sizeof(arr)/sizeof(arr[0]);
int i = 0;

int o[SIZE],e[SIZE];
int oi=0,ei=0;
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
e[ei]=arr[i];
ei++;
}
else
{
o[oi]=arr[i];
oi++;
}
AJIT PANT MSM ENROLLMENT NO 18312004 BATCH S2

}
cout<<”Even \n";
for(int i=0;i<ei;i++)
cout<<e[i]<<endl;
cout<<”Odd \n";
for(int i=0;i<oi;i++)
cout<<o[i]<<endl;
return 0;
}
Output:
Even
12
34
8
90
Odd
45
9
3

3. Write a function descendingSort of type void which uses bubblesort


technique to sort an integer array in descending order. Write main to test this
function and to display the unsorted and sorted array. Program should be run
time efficient.
Answer
#include <iostream>

void PrintArray(int array[], int n)


AJIT PANT MSM ENROLLMENT NO 18312004 BATCH S2

{
for (int i = 0; i < n; ++i)
cout << array[i] << " " << flush;
cout << endl;
}

void descendingSort(int array[], int n)


{
bool swapped = true;
int j = 0;
int temp;

while (swapped)
{
swapped = false;
j++;
for (int i = 0; i < n - j; ++i)
{
if (array[i] < array[i + 1])
{
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swapped = true;
}
}
AJIT PANT MSM ENROLLMENT NO 18312004 BATCH S2

}
}

int main() {
int array[] = {94, 42, 50, 95, 333, 65, 54, 456, 1, 1234};
int n = sizeof(array)/sizeof(array[0]);

cout << "Before Bubble Sort :" << endl;


PrintArray(array, n);

descendingSort(array, n);

cout << "After Bubble Sort :" << endl;


PrintArray(array, n);
return (0);
}
OUTPUT
Before Bubble Sort :
94 42 50 95 333 65 54 456 1 2325
After Bubble Sort :
1 42 50 54 65 94 95 333 456 2325

4. Write a computer program to process two 2-dimensional arrays. The arrays


use and test the following functions: void ReadMatrix ( int x[ ][10], int SIZE) to
read the elements , void ShowMatrix ( int x[] [ 10], int SIZE) to display the
elements , ' void AddMatrices to add two, two-dimensional arrays and store
AJIT PANT MSM ENROLLMENT NO 18312004 BATCH S2

the result in third, a void MultMatrices to multiply two, two-dimensional


arrays. Write main to test the function. Program should check the conditions
for addition and multiplication of two matrices.
Answer
#include<iostream>
#define SIZE 2
using namespace std;

void ReadMatrix(int x[SIZE][SIZE])


{
for (int i = 0; i < SIZE ; i++)
{
for (int j = 0; j < SIZE; j++)
{
cin>>x [i][j];
}

}
cout << endl;
}

void ShowMatrix(int x[SIZE][SIZE])


{
for (int i = 0; i < SIZE ; i++)
{
for (int j = 0; j < SIZE; j++)
{
cout<<x [i][j]<<” “;

}
cout<<endl;5l
}
cout << endl;
}
void AddMatrices (int myarray1[SIZE][SIZE], int myarray2[SIZE][SIZE] ,int
result[SIZE][SIZE])
{
for (int i = 0; i < SIZE ; i++)
{
AJIT PANT MSM ENROLLMENT NO 18312004 BATCH S2

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


{
result[i][j] = myarray1 [i][j] + myarray2 [i][j];

return;
}

void MultMatrices(int mat1[SIZE][SIZE], int mat2[SIZE][SIZE], int res[SIZE][SIZE])


{
int i, j, k;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
res[i][j] = 0;
for (k = 0; k < SIZE; k++)
res[i][j] += mat1[i][k]*mat2[k][j];
}
}
}

int main ()
{
int x [SIZE][SIZE];
int y [SIZE][SIZE];
int r[SIZE][SIZE];
cout<<”Enter First matrix\n";
ReadMatrix(x);
cout<<”Enter Second matrix\n";
ReadMatrix(y);
AddMatrices (x,y,r);
cout<<”Addition of the matrices\n";
ShowMatrix(r);
MultMatrices(x,y,r);
cout<<”Multiplication of the matrices\n";
ShowMatrix(r);
AJIT PANT MSM ENROLLMENT NO 18312004 BATCH S2

return 0; Commented [AP1]:

}
Output:
Enter First matrix
12
12
Enter Second Matrix
13
13
Addition of the matrices
25
25
Multiplication of the matrices
39
39

5. Write a code in C++, to read a square matrix and to rotate the square matrix
in ' clockwise manner by 90 degrees.
Answer
#include<iostream>
#define n 2
using namespace std;
int main()
{
int mat[n][n];
for (int i=0;i<n;i++)
for(int j=0;j<n;j++)
AJIT PANT MSM ENROLLMENT NO 18312004 BATCH S2

cin>>mat[i][j];
int rot[n][n];
cout<<”Rotated"<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
rot[i][j]=mat[n-1-j][i];
cout<<rot[i][j]<<” “;
}
cout<<endl;
}

return 0;
}
Output:
12
34
Rotated
31
42
6. Write a program in C++ that uses/ describe the following: getchar( ).
putchar( ), puts, gets, difference between cunt and puts difference between
cin and gets, get ( ), put( ), getline( ) and write( ).
#include<iostream>
#include<studio.h>
#include<string>
AJIT PANT MSM ENROLLMENT NO 18312004 BATCH S2

using namespace std;


int main()
{
puts ("Enter text\n”);
char ch;
ch=getchar();
putchar(ch);
cin.get(ch);
cout.put(ch);
char c[10];
gets(c);
cout<<c;
string str;
getline(cin,str);
cout.write(std,10);
return 0;

}
Output
Enter text
C
C
E
E
Hello
Hello
Hi Sir
Hi Sir
7. Write programs in C++ that uses/ describe the functions involved in “string”.
Answer
AJIT PANT MSM ENROLLMENT NO 18312004 BATCH S2

#include <iostream>
#include<string.h>
using namespace std;

string returnFloatingPart(string str)


{
int pos = str.find(".");
if (pos == string::npos)
return "";
else
return str.substr(pos + 1);
}

bool containsOnlyDigit(string str)


{
int l = str.length();
for (int i = 0; i < l; i++)
{
if (str.at(i) < '0' || str.at(i) > '9')
return false;
}

return true;
}
AJIT PANT MSM ENROLLMENT NO 18312004 BATCH S2

string replaceBlankWith20(string str)


{
string replaceby = "%20";
int n = 0;
while ((n = str.find(" ", n)) != string::npos )
{
str.replace(n, 1, replaceby);
n += replaceby.length();
}
return str;
}

int main()
{
string fnum = "23.342";
cout << "Floating part is : " << returnFloatingPart(fnum) << endl;

string num = "3452";


if (containsOnlyDigit(num))
cout << "string contains only digit" << endl;

string urlex = "google com in";


cout << replaceBlankWith20(urlex) << endl;

return 0;
}
AJIT PANT MSM ENROLLMENT NO 18312004 BATCH S2

Output
Floating part is : 342
string contains only digit
google%20com%20in

You might also like