In: Computer Science
C++ programming
please i need all codes in this homework
Homework
1) Call foo() three times to print below.
1 2
3 4
5 6
2) Modify the calculator in lect16 as follows using argument-passing functions.
....................
// function prototypes
void show_menu();
void add(int x, int y);
void sub(int x, int y);
.............
// function definitions
void main(){
int s;
for(;;){
show_menu();
scanf(“%d”, &s);
if (s==1){
int x, y;
printf(“enter two numbers\n”);
scanf(“%d %d”, &x, &y);
add(x, y);
}else if (s==2){
...............
}
.................
}
}
void show_menu(){
printf(“1. add 2. sub 3. square 4. factor_of 5. power 6. factor 7. quit\n”);
printf(“select operation\n”);
}
void add(int x, int y){
// print x+y
int z;
z=x+y;
printf(“the sum is %d\n”, z);
}
void sub(int x, int y){
// print x-y
.........
}
void square(int x){
// print x*x
........
}
void power(int x, int y){
// print x*x*...*x (y times)
........
}
void factor_of(int x, int y){
// if x is a factor of y, print “x is a factor of y”
// otherwise “x is not a factor of y”
// for example if x=3, y=12, x is a factor of y
// because 12%3 = 0
.......
}
void factor(int x){
// display all factors of x
...................
}
Answer 1:
#include<iostream.h>
#include<conio.h>
void foo(){
static int x=1;
cout<<x<<"\t"<<x+1<<endl;
x=x+2;
}
int main()
{
clrscr();
foo();
foo();
foo();
getch();
return 0;
}
Answer 2:
#include<stdio.h>
#include<conio.h>
void show_menu();
void add(int x, int y);
void sub(int x, int y);
void square(int x);
void power(int x,int y);
void factor_of(int x,int y);
void factor(int x);
int i;
// function definitions
void main(){
int s;
while(1){
show_menu();
scanf("%d", &s);
if (s==1){
int x, y;
printf("enter two numbers\n");
scanf("%d %d", &x, &y);
add(x, y);
}else if (s==2){
int x, y;
printf("enter two numbers\n");
scanf("%d %d", &x, &y);
sub(x, y);
}
else if(s==3){
int x;
printf("Enter number: ");
scanf("%d",&x);
square(x);
}
else if(s==4){
int x, y;
printf("enter two numbers\n");
scanf("%d %d", &x, &y);
factor_of(x, y);
}
else if(s==5){
int x, y;
printf("enter two numbers\n");
scanf("%d %d", &x, &y);
power(x, y);
}
else if(s==6){
int x;
printf("Enter number: ");
scanf("%d",&x);
factor(x);
}
else{
break;
}
}
}
void show_menu(){
printf("1. add 2. sub 3. square 4. factor_of 5. power 6. factor 7.
quit\n");
printf("select operation\n");
}
void add(int x, int y){
// print x+y
int z;
z=x+y;
printf("the sum is %d\n", z);
}
void sub(int x, int y){
printf("the subtraction is %d\n",(x-y));
}
void square(int x){
printf("the square of %d is %d\n",x,(x*x));
}
void power(int x, int y){
int temp=x;
int i;
for(i=0;i<y;i++){
x=x*x;
}
printf("%d to power %d is :%d\n",temp,y,x);
}
void factor_of(int x, int y){
// if x is a factor of y, print “x is a factor of y”
// otherwise “x is not a factor of y”
if(y%x==0){
printf("%d is a factor of %d\n",x,y);
}
else{
printf("%d is not a factor of %d\n",x,y);
}
}
void factor(int x){
printf("all the factors of %d is: \n",x);
for(i=1;i<=x;i++){
if(x%i==0){
printf("%d\t",i);
}
}
}
output:
