This Question is from my C programming class. Book that we use is Ansi C 4th Edi
ID: 3824240 • Letter: T
Question
This Question is from my C programming class. Book that we use is Ansi C 4th Edition by Gary J Bronson. I'm on Chapter 7 currently.
Your task is to write a program that has a function. It should have these general characteristics: The program should not be longer than 30 lines. This should be a small task, nothing fancy. The program must contain a function. The task of the function is to be passed in a minimum of two variables and to change their value within the function. The main program will declare the variables (not global variables) and initialize them to some starting value, call the function, and print out the values of the variables after the function. You will need to have a prototype of the function. The variables can be char, int, or float.
Explanation / Answer
#include <stdio.h>
void square(int &a, int &b);
int main()
{
int a = 5, b =10;
printf("Before calling values a = %d , b = %d ",a,b);
square(a,b);
printf("After calling values a = %d , b = %d ",a,b);
return 0;
}
void square(int &a, int &b) {
a = a * a;
b =b * b;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Before calling values a = 5 , b = 10
After calling values a = 25 , b = 100
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.