Question

In: Computer Science

C++ programming please i need all codes in this homework Homework 1) Call foo() three times...

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

    ...................

}

Solutions

Expert Solution

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:


Related Solutions

I need this in C# please. Exercise #2: Design and implement a programming (name it NextMeeting)...
I need this in C# please. Exercise #2: Design and implement a programming (name it NextMeeting) to determine the day of your next meeting from today. The program reads from the user an integer value representing today’s day (assume 0 for Sunday, 1 for Monday, 2 for Tuesday, 3 for Wednesday, etc…) and another integer value representing the number of days to the meeting day. The program determines and prints out the meeting day. Format the outputs following the sample...
I need C++ programming with output. I have tried other programming and it does not work....
I need C++ programming with output. I have tried other programming and it does not work. So please give me the one that actually works. Assignment 1 Design your own linked list class that works as a template class. It should provide member functions for appending, inserting and deleting nodes. The destructor should destroy the list. The class should also provide a member function that will display the contents of the list to the screen. The class should also provide...
(I have posted this question three times without any answer! please answer it. I really need...
(I have posted this question three times without any answer! please answer it. I really need the answer) Using openERP open-source (Odoo 10) systems, you will have to build HR & Finance business processes into the system. You will have to build ( a University or any company of your choosing) HR business processes using one of the tool you found interesting. You will have to find out improvements of the process Add the business rules to the processes You...
Subject: Homework - Family Trees please give me C codes, thanks FAMILY TREE In a Binary...
Subject: Homework - Family Trees please give me C codes, thanks FAMILY TREE In a Binary Tree, each node has two children at most. To eliminate the restriction by linking the children together to form a list. In this design, each node in the tree needs to contain only two pointers: one to its eldest child and one to its next younger sibling. Using this representation, in each node, the pointer on the left always points down to child; the...
I need an answer to all three questions, please. Q1: Which of the following was not...
I need an answer to all three questions, please. Q1: Which of the following was not a policy response to the Economic crisis associated with the COVID-19 Crisis? Select one: a. The Fed lowered the policy rate by roughly 1.5 percentage points to nearly zero. b. The U.S. Congress passed a roughly $3 Trillion economic stimulus package. c. The Commonwealth of Massachusetts relaxed the balanced budget rule in the State Constitution. d. The Fed introduced facilities to keep markets liquid...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
FOR ARDUINO PROGRAMMING NEED TO HAVE ALL LEDS FLASH 5 TIMES WHEN THE BOARD STARTS.
FOR ARDUINO PROGRAMMING NEED TO HAVE ALL LEDS FLASH 5 TIMES WHEN THE BOARD STARTS.
Note: I need a code and other requirement Note: programming language is c++ If you need...
Note: I need a code and other requirement Note: programming language is c++ If you need more information, please clarify what information you want. consider solving the equation sin(x) - e^(-x) = 0 write a computer program to solve the given equation using: 1- bisection method 2- fixed-point method 3- newton's intervals: {0,1},{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,10} choose accuracy E = 10^(-5) Make sure you document your program Requirement : 1- Mathematical justification 2- algorithem description 3- code (program) with documentation 4-output: roots ,...
I am building a game in C programming language where I need to add objects of...
I am building a game in C programming language where I need to add objects of various length into a game board. The game board is 8X8 and we must account for the boundaries for the board and not go over them with our objects. The boards upper left corner is at 0x0 and we must return 1 if it fits and -1 if it does not fit. I have the following 2 functions to start with: ```int add_object_vert(int r,...
USE C PROGRAMMING, call all functions in main, and use a 2 by 3 2d array...
USE C PROGRAMMING, call all functions in main, and use a 2 by 3 2d array to test, thanks. get_location_of_min This function takes a matrix as a 2-D array of integers with NUM_COLS width, the number of rows in the matrix and two integer pointers. The function finds the location of the minimum value in the matrix and stores the row and column of that value to the memory location pointed to by the pointer arguments. If the minimum value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT