Assignment # 4 Q

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Programming Fundamentals

Assignment # 04
NAME HAMAYUN SAEED
ROLL NO 19014156-019
Questions
1. Write a program that takes a number from user and pass it to a function. That
function should return the square of the number.
#include<iostream>
using namespace std;
int square(int i);
int main(){
int j;
cout<<"entre a number=";
cin>>j;
square (j);
return 0;
}
int square(int i){
int a;
a=i*i;
cout<<"square of the number is="<<a;
return a;
}

2. Write a program that takes a number from user and pass it to a function. That
function should return the cube of the number.
#include<iostream>
using namespace std;
int cube(int b);
int main(){
int a;
cout<<"entre a number=";
cin>>a;
cube(a);
return 0;

}
int cube(int b){
int m;
m=b*b*b;
cout<<"cube of the number is="<<m;
return m;

3.Write a program that inputs two number from user and pass it to a function. That
function should return the power of number as first number raised to power second
number
#include<iostream>
#include<math.h>
using namespace std;
int po(int r,int t);
int main(){
int a,b;
cout<<"entrefirst number=";
cin>>a;
cout<<"entre second number=";
cin>>b;
pow(a,b);
return 0;
}
int po(int r,int t){
int power=pow (r,t);
cout<<r<<"power is="<<power;
return 0;
}
.
1. Write a program that implements the functionality of Question 1, 2 and 3 in a
single program. The program should take a choice from user for a particular
operation (e.g. square, cube or power). Then call respective function to get the
result.
#include<iostream>
#include<math.h>
using namespace std;
intpo();
intsq();
int cu();
int main()
{
int a;
cout<<"press 1 for square:\npress 2 for cube:\npress3 for power:\n";
cin>>a;
if(a==1){
cout<<"square is"<<sq();
}
else if(a==2){
cout<<"cube is"<<cu();
}
else if(a==3){
cout<<"power is"<<po();
}
return 0;
}
intpo(){
inta,b,power;
cout<<"enter 1st number:";
cin>>a;
cout<<"enter 2nd number:";
cin>>b;
power=pow(a,b);
return power;
}
intsq(){
inte,square;
cout<<"enter 1st number:";
cin>>e;
square=e*e;
return square;
}
int cu(){
intr,cube;
cout<<"enter 1st number:";
cin>>r;
cube=r*r*r;
return cube;
}

5.Write a program that performs the addition of minimum 2 numbers and


maximum10 numbers. The program should have a user-defined function for this
purpose. That function will accept max 10 numbers and min 2 numbers in parameter
and then return their sum. For example if I pass 2 parameters to this function, it
should return sum of two digits. If I pass 7 digits, it should return sum of 7 digits and
vice versa.
#include<iostream>
using namespace std;
int sum(int aa[]);
int a;
int main()
{
cout<<"enter how many values you want to add:";
cin>>a;
if(a>=2&&a<=10){
intarr[a];
for(inti=0;i<a;i++){
cout<<"enter values:"<<i+1<<" ";
cin>>arr[i];
}
cout<<"sum is:"<<sum(arr);
}
else
cout<<"you can not add more than 10 values";
return 0;
}
int sum(int aa[])
{
intsumm=0;
for(inti=0;i<a;i++){
summ+=aa[i];
}
returnsumm;
}

Hint: Use Default Parameters


6.Implement the above program from Question 5 for subtraction of 2 to 10 numbers.
#include<iostream>
using namespace std;
int sub(int aa[]);
int a;
int main()
{
cout<<"enter how many values you want to subtract:";
cin>>a;
if(a>=2&&a<=10){
intarr[a];
for(inti=0;i<a;i++){
cout<<"enter values:"<<i+1<<" ";
cin>>arr[i];
}
cout<<"sum is:"<<sub(arr);
}
else
cout<<"you can not subtract more than 10 values";
return 0;
}
int sub(int aa[])
{
intsubb=0;
for(inti=0;i<a;i++){
subb-=aa[i];
}
returnsubb;
}

7.Implement the above program from Question 5 for multiplication of 2 to 10


numbers. #include<iostream>
using namespace std;
intmul(int aa[]);
int a;
int main()
{
cout<<"enter how many values you want to multiply:";
cin>>a;
if(a>=2&&a<=10){
intarr[a];
for(inti=0;i<a;i++){
cout<<"enter values:"<<i+1<<" ";
cin>>arr[i];
}
cout<<"multiply is:"<<mul(arr);
}
else
cout<<"you can not multiply more than 10 values";
return 0;
}
intmul(int aa[])
{
intmult=1;
for(inti=0;i<a;i++){
mult*=aa[i];
}
returnmult;
}
8.Write a program that implements the functionality of programs asked in Question 5,
6 and 7 in a single program. The program should take a choice from user for a
particular operation (e.g. Addition, Subtraction or Multiplication). Then call respective
function to get the result. #include<iostream>
using namespace std;
intmul(int aa[]);
int sum(int aa[]);
int sub(int aa[]);
int a;
int main()
{ int choice;
cout<<"press 1 for addition\npress 2 for subtraction\npress 3 for multiplication\n";
cin>>choice;
if(choice==1){
cout<<"enter how many values you want to sum:";
cin>>a;
if(a>=2&&a<=10){
intarr[a];
for(inti=0;i<a;i++){
cout<<"enter values:"<<i+1<<" ";
cin>>arr[i];
}
cout<<"sum is:"<<sum(arr);
}
else
cout<<"you can not add more than 10 values";
}
else if(choice==2){
cout<<"enter how many values you want to Subtract:";
cin>>a;
if(a>=2&&a<=10){
intarr[a];
for(inti=0;i<a;i++){
cout<<"enter values:"<<i+1<<" ";
cin>>arr[i];
}
cout<<"Subtract is:"<<sub(arr);
}
else
cout<<"you can not subtract more than 10 values";
}
else if(choice==3){
cout<<"enter how many values you want to multiply:";
cin>>a;
if(a>=2&&a<=10){
intarr[a];
for(inti=0;i<a;i++){
cout<<"enter values:"<<i+1<<" ";
cin>>arr[i];
}
cout<<"multiply is:"<<mul(arr);
}
else
cout<<"you can not multiply more than 10 values";
}
return 0;
}
intmul(int aa[])
{
intmult=1;
for(inti=0;i<a;i++){
mult*=aa[i];
}
returnmult;
}
int sum(int aa[])
{
intsumm=0;
for(inti=0;i<a;i++){
summ+=aa[i];
}
returnsumm;
}
int sub(int aa[])
{
intsubb=0;
for(inti=0;i<a;i++){
subb-=aa[i];
}
returnsubb;
}

9.Write a program that takes two numbers from user and pass them to a function.
That function should swap their values. If I print those numbers in main function
after calling the SWAP function, their swapped values should be displayed.
#include<iostream>
using namespace std;
void swap(intx,int y);
int main()
{
inta,b;
cout<<"enter two numbers:";
cin>>a>>b;
cout<<"before swapping"<<endl;
cout<<"a ="<<a<<endl;
cout<<"b ="<<b<<endl;
swap(a,b);
return 0;
}
void swap(intx,int y){
int z;
z=x;
x=y;
y=z;
cout<<"after swapping"<<endl;
cout<<"a ="<<x<<endl;
cout<<"b ="<<y<<endl;
}

10.Write a program inputs 10 elements in a 1D array. Pass this array to a function


that should return the max and min value in the array.
#include<iostream>
using namespace std;
intmaximin(intacc[]);
int main()
{
intarr[10]={12,13,45,67,89,68,90,40,80,11};
maximin(arr);
return 0;
}
intmaximin(intacc[]){
intmax,min;
max=acc[10];
for(inti=0;i<10;i++){
if(max<acc[i]){
max=acc[i];
}
cout<<"max:"<<max<<endl;
}
min=acc[10];
for(inti=0;i<10;i++){
if(min<acc[i]){
min=acc[i];
}
cout<<"min:"<<min<<endl;
}
}

11.Write a program that takes 20 elements in a 1D array. Pass this array to a


function. That function should print the no of 1s and 0s in that array.
#include<iostream>
using namespace std;
void search(intarr[], int n)
{
int count = 0;
for (inti = 0; i< n; i++) {
if (arr[i] == 0)
count++;
}
for (inti = 0; i< count; i++)
arr[i] = 0;
for (inti = count; i< n; i++)
arr[i] = 1;
}
void p(intarr[], int n)
{
cout<< "Array after search is ";

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


cout<<arr[i] << " ";
}
int main()
{
intarr[] = { 0, 1, 0, 1, 1, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
search(arr, n);
p(arr, n);

return 0;
}

12.Write a program that takes 10 elements in a 1D array and pass it to a function


that should return max value in that array. Then pass this returned value to another
function that should return true if that value is a prime number and false if it is
composite.
#include<iostream>
using namespace std;
int max(intacc[]);
intpr(intnum);
int main()
{
intarr[10];
cout<<"enter a number:";
for(inti=0;i<10;i++)
cin>>arr[i];
max(arr);
pr(max);
return 0;
}
int max(intacc[]){
int max;
max=acc[10];
for(inti=0;i<10;i++){
if(max<acc[i]){
max=acc[i];
}

}
cout<<"max:"<<max<<endl;
return max;
}
intpr(intnum){
for(i = 2; i<= n / 2; ++i)
{
if(n % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
cout<< "This is a prime number";
else
cout<< "This is not a prime number";
return 0;
}

13.Write a program that inputs a matrix of 4*2. Pass it to a function that should
return the sum of all elements of that matrix.
#include<iostream>
using namespace std;
int sum(intarr[4][2]);
int main()
{
int arr1[4][2];
for(inti=0;i<4;i++){
for(int j=0;j<2;j++){
cout<<"enter values of array:["<<i<<"]["<<j<<"]";
cin>>arr1[i][j];
}
}
cout<<"sum is:"<<sum(arr1);
return 0;
}
int sum(intarr[4][2]){
inti,j,summ=arr[0][0];
for(i=0;i<4;i++){
for(j=0;j<2;j++){

}
summ=arr[i][j];
returnsumm;
}..

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