Question

Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin,...

Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin, cout, cin.getline) .

Re-make the func function by the following prototype:

  • void func( double, double &);

#include
int user_interface();
double func(double);
void print_table();


int main (int argc, char *argv[]) {

   print_table(user_interface());


   return 0 ;

}


int user_interface(int val){
   int input = 0;
   printf("This function takes in x and returns an output\n");
   printf("Enter Maximum Number of X:");
   scanf("%d", &input);
   while(input <= 0) {
       printf("integers only\n");
       while(getchar() != '\n');
       scanf("%d", &input);
   }
   return input;
}
double func(double x){
   int result;
   if(x<5){
       result = x*x;
   }
   else if ( x < 10 ) {
       result=x*x - 3*x -10;
   }
   else {
       result = x*x -7*x -8 ;
   }
   return result;
}


void print_table(int val){
   double x;

   printf("%7c%14c\n",'x','y');
   for (x = 0 ; x < val ; x +=.5 ) {
       printf("%10.2f%14.2f\n", x , func(x));
   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The C++ equivalent for your code is as:

************************myProg.cpp***************************

#include <iostream>
#include <iomanip>

using namespace std;

int user_interface();
void func(double,double&);
void print_table(int);


int main (int argc, char *argv[]) {

print_table(user_interface());


return 0 ;

}


int user_interface(){
int input = 0;
cout<<"This function takes in x and returns an output"<<endl;
cout<<"Enter Maximum Number of X:"<<endl;
cin>>input;
while(input <= 0) {
cout<<"integers only"<<endl;
//clear the buffer
cin.sync();
cin>>input;
}
return input;
}
void func(double x,double &result){

if(x<5){
result = x*x;
}
else if ( x < 10 ) {
result=x*x - 3*x -10;
}
else {
result = x*x -7*x -8 ;
}

}


void print_table(int val){
double x;
double result;
cout<<"\tx\t\t\ty"<<endl;
for (x = 0 ; x < val ; x +=.5 ) {
   func(x,result);

   cout<<"\t"<<fixed<<setprecision(2)<<x<<"\t\t\t"<<result<<endl;

}

}

Add a comment
Know the answer?
Add Answer to:
Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...

  • I'm having trouble getting a certain output with my program Here's my output: Please input a...

    I'm having trouble getting a certain output with my program Here's my output: Please input a value in Roman numeral or EXIT or quit: MM MM = 2000 Please input a value in Roman numeral or EXIT or quit: mxvi Illegal Characters. Please input a value in Roman numeral or EXIT or quit: MXVI MXVI = 969 Please input a value in Roman numeral or EXIT or quit: EXIT Illegal Characters. Here's my desired output: Please input a value in...

  • Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS program that will...

    Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS program that will read in a base (as an integer) and a value (nonnegative integer but as an ASCII string) in that base and print out the decimal value; you must implement a function (which accepts a base and an address for a string as parameters, and returns the value) and call the function from the main program. The base will be given in decimal and will...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • C programming Question1 (a) Write a C program that will print out all command line arguments,...

    C programming Question1 (a) Write a C program that will print out all command line arguments, in reverse order, one per line. Prefix each line with its index. 6 marks] (b) Consider this C code snippet int a- 100 int b- 42; inte p- &a; int q-b; p qi printf ("%d %d\n" ,a,*p); When this code is executed, what numbers will it print? [2 marks] (c) Consider this C program int main(int argc,char argv) char* target- "Apple" char vord[100] printf...

  • What are the description of approach or technique used in this? #include <stdio.h> long charCount=0; long...

    What are the description of approach or technique used in this? #include <stdio.h> long charCount=0; long digitCount=0; long lineCount=0; long wordCount=0; long digitFreq[10]; int c=0; int outOfWord=0; int state=0; int inWord=1; void printStats(); void totalWords(); void totalLines(); void digitFrequency(); int main(int argc,char **argv) { int i; for(i=0;i<10;i++) { digitFreq[i]=0; } state=outOfWord; c=getchar(); while((c !=EOF)) { charCount++; digitFrequency(c); totalLines(c); totalWords(c); c=getchar(); } printStats(); return 1; } void totalLines(int c) { if(c == '\n') { lineCount++; } } void digitFrequency(int c) {...

  • please rewrite or convert this C program into Assembly language and please make a notice of...

    please rewrite or convert this C program into Assembly language and please make a notice of which function is which! #include <stdio.h> #include <stdlib.h> void printMonthYear(int mon,int year); void printDaysOfWeek(); void printDays(int mon,int year); int getFirstDayOfMonth(int mon, int year); int getNumOfDaysInMonth(int mon, int year); int main(void) {    int mon=-1; int year= -1; printf("Month : "); fflush(stdout); scanf("%d",&mon);    if(mon < 1|| mon > 12){ printf("Invalid month >.<!: %d ! Must be between 1 and 12\n",mon); return 1; } printf("Year...

  • Programming C....... function code is clear but compile is wrong .. I input 8 and compiled...

    Programming C....... function code is clear but compile is wrong .. I input 8 and compiled 2 0 1 1 2 3 5 8 13.. look first number 2 is wrong. The correct answer 8 is 0 1 1 2 3 5 8 13 21.. Need fix code to compile correctly.. Here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[1000]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for (...

  • Modify the program pls so it then asks the user for another input making the program ask for a ye...

    modify the program pls so it then asks the user for another input making the program ask for a yes or no question looping it codeblocks #include <stdio.h> #include <ctype.h> #define NEWLINE '\n' int main(void) {    /* Declare variables and function prototypes. */    int k=0, formula[20], n, current=0, done=0, d1, d2;    double error=0, weight, total=0;    double atomic_wt(int atom);    /* Read chemical formula from keyboard. */    printf("Enter chemical formula for amino acid: \n");    while ((formula[k]=getchar()) != NEWLINE)       k++;    n = k;   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT