Question

Trace the following program, showing the values of the variables and the output generated by the...

Trace the following program, showing the values of the variables and the output generated by the program. The input file to the program is:

1375 3

8472 2

(SHOW YOUR WORK)

#include <stdio.h>

#define BASE 10

int find( int, int);

int extract( int, int);

main()

{

int a, b; int c, d;

while( scanf("%d", &a) != EOF)

{ scanf("%d", &b);

c = find(a, b);

if(c >= 0)

d = extract(a, c);

else d = c;

printf("its %d \n", d);

}

}

int find(int a, int b)

{ int i = 0;

while( a % BASE != b)

{ a = a / BASE;

if(a == 0) return -1;

i = i + 1;

}

return i;

}

int extract(int a, int b)

{

while(b)

{

a = a / BASE;

b = b - 1;

}

return a % BASE;

}

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

/* Output of above program with sample input give as above is as follows */

/* Program tracing to find values of variables */

/*Changes done in above code to show above output are marked in bold in below code */

#include <stdio.h>

#define BASE 10

int find( int, int);

int extract( int, int);

int main()

{

  

int a, b; int c, d;

  

while( scanf("%d", &a) != EOF)

  

{ scanf("%d", &b);

  

c = find(a, b);

  

printf("\n");

printf("value of c is %d \n",c);

  

if(c >= 0){

  

d = extract(a, c);

printf("value of d if c is greater than zero is %d \n",d);

  

}

else d = c;

  

printf("its %d \n", d);

  

}

  

}

int find(int a, int b)

{ int i = 0;

  

while( a % BASE != b)

  

{ a = a / BASE;

  

if(a == 0) return -1;

  

i = i + 1;

  

}

  

return i;

  

}

int extract(int a, int b)

{

  

while(b)

  

{

  

a = a / BASE;

  

b = b - 1;

  

}

  

return a % BASE;

  

}

Add a comment
Know the answer?
Add Answer to:
Trace the following program, showing the values of the variables and the output generated by the...
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
  • Part D. On executing the below program, what will be the output of the following program...

    Part D. On executing the below program, what will be the output of the following program if the source file contains a line "This is a sample."? [5 marks] #include <stdio.h> int main() { char ch; int k, n=0; FILE * fs = fopen("source.txt", "r"); while(1) { ch = fgetc(fs); if(ch == EOF) break; else { for(k=0;k<4;k++) fgetc(fs); printf("%c", ch); n += k; } } printf("%d\n", n); fclose(fs); return 0; } ---------------------------------- Any one can explain the question to me...

  • Write a MATLAB program that implements the algorithm designed in the Topic 1 "Non-Linear Flowchart" and...

    Write a MATLAB program that implements the algorithm designed in the Topic 1 "Non-Linear Flowchart" and "Creating a Flowchart" assignments previously implemented in C. Compare and contrast the C and MATLAB versions of your codes. convert this to a matlab program #include <stdio.h> int main() { int a; printf("Enter first number: "); scanf("%d", &a); int b; printf("Enter second number: "); scanf("%d", &b); int limit; printf("Enter limit: "); scanf("%d", &limit); printf("\nFirst number: %d\n", a); printf("Second number: %d\n", b); int c; c...

  • C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string usi...

    C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only). It can contain ONLY the following libraries: stdio, stdlib, math and string. I want to use the following function prototypes to try and implement things: /*Check whether a given number is prime or not*/ int checkPrime(int n) /*to find gcd*/ int gcd(int a, int h) void Encrypt(); void Decrypt(); int getPublicKeys(); int getPrivateKeys();...

  • Create another program that will prompt a user for input file. The user will choose from...

    Create another program that will prompt a user for input file. The user will choose from 4 choices: 1. Display all positive balance accounts. 2. Display all negative balance accounts. 3. Display all zero balance accounts. 4. End program. C PROGRAMMING my code so far #include<stdlib.h> #include<stdio.h> int main(void) { int request; int account; FILE *rptr; char name[20]; double balance; FILE *wptr;    int accountNumber;    if((wptr = fopen("clients.dat","w")) == NULL) { puts("File could not be opened"); } else {...

  • Given the following C program, rewrite the program and use C++ header files, C++ constants, inline...

    Given the following C program, rewrite the program and use C++ header files, C++ constants, inline functions and C++ I/O. Please note that the standard C++ library header file, which defines the I/O functions is named <iostream>. This header file contains the definition of the std namespace. /* Convert this program into a C++ program. */ #include <stdio.h> #include <conio.h> #define PI 3.141592 float reactance(float c,float f) { return 1/(2*PI*f*c); } int main() { float frq,cap,xc; printf("Enter frequency => ");...

  • C Programming I was given this code to display the following as the output but it...

    C Programming I was given this code to display the following as the output but it does not seem to work. what is wrong? or can someone guide me through the steps? thanks! I have created the txt file named myfile.txt but the program will not compile. please help. I am forced to use Microsoft visual studio. This program makes a duplicate copy of a file (reads input from a file and write output to another file) 1 #include <stdio.h>...

  • Translate the following C program to Pep/9 assembly language. #include <stdio.h> int main() {     int...

    Translate the following C program to Pep/9 assembly language. #include <stdio.h> int main() {     int number;     scanf("%d", &number);     if (number % 2 == 0) {         printf("Even\n");     }     else {         printf("Odd\n");     }     return 0; }

  • Write a C program by completing the following skeleton program. /* file minmax.c  **************************************** *   ...

    Write a C program by completing the following skeleton program. /* file minmax.c  **************************************** *        a C program skeleton for the problem *        Find the minimum and maximum values in an array **********************************************************/ #include <stdio.h> /* DATA segment: Global variables */ #define SIZE 10         int arr[SIZE] = { -5, 0xffffface, 0x31, 52, 054, /* base 8 */                         0,...

  • Hello, I need help with the following C code. This program asks the user to enter...

    Hello, I need help with the following C code. This program asks the user to enter three numbers and outputs the sum on the screen , all im trying to do is have the program reprompt the user for a number if anything other than a number is entered for each of the entries. I wanted to use do while for each of the functions but that did not work for example: Enter Number 1: 2 Enter Number 2: Hello...

  • 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...

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