# Need Help with Homework.

This is a discussion on Need Help with Homework. within the C Programming forums, part of the General Programming Boards category; I have an assignment due in Programming 1 and I need a little bit of help. I have already created ...

1. ## Need Help with Homework.

I have an assignment due in Programming 1 and I need a little bit of help. I have already created the program but now I have one last thing to do. However, I don't quite understand what my professor wants me to do. Here are the instructions she gave me:

"Use a single subprogram to prompt and read user's input for the numbers to be used in the calculations.
Note: It must be used to get all input for calculations."

I am not sure what that means.

Here is the code:

Code:
#include <stdio.h>
#include <math.h>

/*
Stephen Haroldson
11:00am
*/

int main(void) {
int selection,num1,num2,ans,fans;
while(selection != 8)  {
switch(selection) {
case 1: printf("Enter 1st number: ");
scanf("&#37;d", &num1);
printf("Enter 2nd number: ");
scanf("%d", &num2);
ans = num1 + num2;
printf("%d + %d = %d\n",num1,num2,ans);
break;
case 2: printf("Enter 1st number: ");
scanf("%d", &num1);
printf("Enter 2nd number: ");
scanf("%d", &num2);
ans = num1 - num2;
printf("%d - %d = %d\n",num1,num2,ans);
break;
case 3: printf("Enter 1st number: ");
scanf("%d", &num1);
printf("Enter 2nd number: ");
scanf("%d", &num2);
ans = num1 * num2;
printf("%d * %d = %d\n",num1,num2,ans);
break;
case 4: printf("Enter 1st number: ");
scanf("%d", &num1);
printf("Enter 2nd number: ");
scanf("%d", &num2);
if(num2 == 0) {
printf("Zero is not an acceptable value!\n");
break; }
fans =(float)num1/num2;
printf("%d / %d = %2d\n",num1,num2,fans);
break;
case 5: printf("Enter 1st number: ");
scanf("%d", &num1);
ans = abs(num1);
printf("|%d| = |%d|\n",num1,ans);
break;
case 6: printf("Enter 1st number: ");
scanf("%d",&num1);
if(num1 < 0){
printf("You cannot enter in a negative value!\n");
break; }
fans = sqrt(num1);
printf("sqrt(%d) = %d\n",num1,fans);
break;
case 7: printf("Enter base: ");
scanf("%d", &num1);
if((num1 == 0)||(num2 <= 0)) {
printf("That is not an acceptable value!\n");
break; }
printf("Enter exponent: ");
scanf("%d", &num2);
fans = pow(num1, num2);
printf("%d ^ %d = %d\n",num1,num2,fans);
break;
default: printf("%d is not a valid selection\n", selection);
}
}
printf("Bye!\n");
}

int choice;
printf("2 Subtract\n");
printf("3 Multiply\n");
printf("4 Divide\n");
printf("5 Absolute Value\n");
printf("6 Square Root\n");
printf("7 Powers\n");
printf("8 Quit\n");
printf("Enter a Selection: ");
scanf("%d", &choice);
return choice;
}
If anyone can help me I would appreciate it a lot

2. What he means there is, you should have function where that is responsible in reading all values from the user any calculation you do in the main. So for example, you have so man scanf in your main function. Create a function and have just one scanf function there and return the read value back to main.

ssharish

3. So I would have to create an if statement for every form of math that the user inputs?
Something like:

Code:
if(symbol = +) {
scanf("%d", "%d", "%d",num1,symbol,num2);
ans = num1 + num2;
printf("%d + %d = %d\n",num1,num2,ans);
}
As far as I understand what your saying...

4. Originally Posted by skaldicpoet9
So I would have to create an if statement for every form of math that the user inputs?
Something like:

Code:
if(symbol = +) {
scanf("%d", "%d", "%d",num1,symbol,num2);
ans = num1 + num2;
printf("%d + %d = %d\n",num1,num2,ans);
}
As far as I understand what your saying...
Not really, no. Look at your original code: more specifically, look at your inputting-of-numbers. Do you see any patterns there? Maybe that repeated code could be made into a routine -- C uses functions for routines as well, so it's going to be a function. So write a function that inputs two numbers. (Note that you can't return two things from a function -- you'll have to use pass-by-pointer with the two variables you want read.)

5. Ok, I think I understand what you are saying but what is a pass-by-pointer?

6. How familiar are you with functions? If you have a function with prototype
Code:
void my_function(int I_am_a_number, int *I_am_a_pointer);
then the first parameter is passed by value, which means a copy is made and the function deals with the copy. The second parameter is passed by pointer (a/k/a passed by reference) which means you are able to directly access the (same) memory in your function.

7. Originally Posted by skaldicpoet9
Ok, I think I understand what you are saying but what is a pass-by-pointer?
Code:
#include <stdio.h>
{
scanf("%d", val);
}
int main()
{
int num;

/* this is pass by pointer */

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

return 0;
}
Look at the sample code above. More specifically look at the whu send the num as a parameter to functon readval. I am sending the address of num to readval. That is what is called pass by pointer.

ssharish

8. Ok, I understand now. Thanks for all of your help

9. Code:
case 4: printf("Enter 1st number: ");
scanf("%d", &num1);
printf("Enter 2nd number: ");
scanf("%d", &num2);
if(num2 == 0) {
printf("Zero is not an acceptable value!\n");
break; }
fans =(float)num1/num2;
printf("%d / %d = %2d\n",num1,num2,fans);
break;
I must stress to you that writing code that way is very bad. Hard to read, easy to miss. Creates bugs. Every block or if should stand out, therefore I recommend you write it this way:
Code:
case 4: printf("Enter 1st number: ");
scanf("%d", &num1);
printf("Enter 2nd number: ");
scanf("%d", &num2);
if(num2 == 0) {
printf("Zero is not an acceptable value!\n");
break;
}
fans =(float)num1/num2;
printf("%d / %d = %2d\n",num1,num2,fans);
break;