Question

Hi, this program is in C. Can you a pass by pointer in this program. Thanks. #include //function ...

Hi, this program is in C.

Can you a pass by pointer in this program. Thanks.

#include
//function declaration/prototype here

int main(int argc, char * argv[]) {
int num1, num2;
  
printf("Please enter two integers (separated by ,):\n");
scanf("%d,%d", &num1, &num2); //enter two integers separated by a comma (,)
  
printf("num1 stores: %d\n", num1);
printf("num2 stores: %d\n", num2);
  
/*make a function call to make sure the followings are true after we call the function
(1) variable num1 stores the larger value after the function is called
(2) variable num2 stores the smaller value after the function is called
*/
  
  
printf("\nAfter calling the function\n");
printf("num1 stores the bigger one: %d\n", num1);
printf("num2 stores the smaller one: %d\n", num2);
  
return 0;
}

//function definition here

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

Here is a code done in C language for the above problem. It uses pass by pointers so as to see the changes reflected in both the functions.

CODE:

#include<stdio.h>
//function declaration/prototype here
void big_small_change(int *x, int *y);

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

   printf("Please enter two integers (separated by ,):\n");
   scanf("%d,%d", &num1, &num2); //enter two integers separated by a comma (,)

   printf("num1 stores: %d\n", num1);
   printf("num2 stores: %d\n", num2);

   /*make a function call to make sure the followings are true after we call the function
   (1) variable num1 stores the larger value after the function is called
   (2) variable num2 stores the smaller value after the function is called
   */
   big_small_change(&num1,&num2);// calling the function by passing the address of the variables num1 and num2

   printf("\nAfter calling the function\n");
   printf("num1 stores the bigger one: %d\n", num1);
   printf("num2 stores the smaller one: %d\n", num2);

   return 0;
}

//function definition here
void big_small_change(int *x, int *y) {
   // here x points to num1 and y points to num2
   int smaller;// the variable that holds the smaller value
   if(!(*x > *y)) {// if the value of x is smaller than that of y
       smaller = *x;// hold the value of x into variable smaller
       *x = *y;// change the value of x by that in y
       *y = smaller;// giving y the smaller value
   }
  
}

OUTPUT:

Please enter two integers (separated by ,): 34,76 num1 stores: 34 num2 stores: 76 After calling the function num1 stores the

Please enter two integers (separated by ,): 12,9 num1 stores: 12 num2 stores: 9 After calling the function num1 stores the bi

Add a comment
Know the answer?
Add Answer to:
Hi, this program is in C. Can you a pass by pointer in this program. Thanks. #include //function ...
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 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);...

  • Run the C program below and complete the table showing all the variables. Add printf function...

    Run the C program below and complete the table showing all the variables. Add printf function calls to obtain the addresses and values of all 13 variables. (Count the array (ca) as 3 variable/values and also include argc and argv in the table). The table must show the Address, Name, Datatype, Scope, and Value of each variable on the stack. (Hint: use the sizeof function to double check your addresses.) Explain how different the actual memory allocations are from what...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • Digit to WordsWrite a C program that asks the user for a two-digit number, then prints...

    Digit to WordsWrite a C program that asks the user for a two-digit number, then prints the English wordfor the number. Your program should loop until the user wants to quit.Example:Enter a two-digit number: 45You entered the number forty-five.Hint: Break the number into two digits. Use one switch statement to print the word for the firstdigit (“twenty,” “thirty,” and so forth). Use a second switch statement to print the word for thesecond digit. Don’t forget that the numbers between 11...

  • In C please Write a function so that the main() code below can be replaced by...

    In C please Write a function so that the main() code below can be replaced by the simpler code that calls function MphAndMinutesToMiles(). Original maino: int main(void) { double milesPerHour, double minutes Traveled; double hours Traveled; double miles Traveled; scanf("%f", &milesPerHour); scanf("%lf", &minutes Traveled); hours Traveled = minutes Traveled / 60.0; miles Traveled = hours Traveled * milesPerHour; printf("Miles: %1f\n", miles Traveled); return 0; 1 #include <stdio.h> 3/* Your solution goes here */ 1 test passed 4 All tests passed...

  • Use only C Program for this problem. Must start with given codes and end with given...

    Use only C Program for this problem. Must start with given codes and end with given code. CHALLENGE ACTIVITY 9.4.3: Input parsing: Reading multiple items. Complete scanf( to read two comma-separated integers from stdin. Assign userlnt1 and userInt2 with the user input. Ex: "Enter two integers separated by a comma: 3,5", program outputs: 3 + 5 = 8 1 #include <stdio.h> HNM 1 test passed int main(void) { int user Int1; int user Int2; All tests passed 000 printf("Enter two...

  • C program help: Write a C program that uses three functions to perform the following tasks:...

    C program help: Write a C program that uses three functions to perform the following tasks: – The first function should read the price of an item sold at a grocery store together with the quantity from the user and return all the values to main(). example: #include void getInput(int *pNum1, int *pNum2); // note: *pNum1 & *pNum2 are pointers to ints int main () {    int numDucks,numCats;    getInput(&numDucks, &numCats); // passing addresses of num1 & num2 to...

  • I need help making my array into a function that can called by the main function...

    I need help making my array into a function that can called by the main function using my program. This is C programming int prime (int x) { int i; i = 2; while (i < x) { if (x % i == 0) return 0; i++; } return 1; } int main (void){ int n; scanf ("%d", &n); if (n < 1) { printf ("Error: at least one data value must be provided.\n"); return 1; } int a[n]; int...

  • ANSWER ASAP PLEASE IN C Duplicate the program below to a new Program 2, and modify...

    ANSWER ASAP PLEASE IN C Duplicate the program below to a new Program 2, and modify Program 2 such that it generates n random virtual addresses between 0 and 232-1 and computes the page number and offset for each address. Here, do not write to the console. Instead, run your program with n = 1000000 random virtual addresses and compute the total CPU time of the task. Report your findings, as well as how you ran your program. Provide commentary...

  • If you already answer this question, please skip, thanks C Programming. Fill in ... This program...

    If you already answer this question, please skip, thanks C Programming. Fill in ... This program will be called with one command line argument that contains a string followed by an asterisk and an integer. Print out the string as many time as indicated by the integer. For example, when called as prog Hi*3, you print HiHiHi. Hint: Look for the '*' starting from the back of the string. len = strlen(arg) gives you the string length. When you have...

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