Programming In C: Purpose: completely develop your own algorithm. develop your o
ID: 670675 • Letter: P
Question
Programming In C:
Purpose:
completely develop your own algorithm.
develop your own functions with appropriate communications between them.
make a user-friendly interactive program
use loops to keep the programming until the user quits
fully document all functions
use top-down design
Numeric tables generated on demand make it easy to understand the relation between ranges of numbers with a short glance. Write a program to generate such tables as needed with a specified operator controlling their values.
Task:
Write a program to generate a table of values based on an operator entered by the user and two numbers.
Let’s start this lab assignment with an example of what your program should do:
Enter low row number: 1
Enter high row number: 4
Enter low column number: 1
Enter high column number: 3
Enter operation: +
+ 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
Your program should support the following operators at the operation prompt:
+ addition
- subtraction
* multiplication
X also multiplication
x also multiplication
/ division
D division, but with integer results
% modulus
p row value raised to the column power
r column root of the row value
h help: display the operators that are supported by your program (this list).
Each of these operations should be done in a function of it's own (you can, of course, use the same one for the multiple multiplications and multiple divisions.)
Your program should support row and column numbers between negative 20 and 20.
Write a function with the following prototype
that can be used as
to get the row and column information from the user. If you choose to, you may add additional parameters to these functions to make your program easier, but the name of the function, return type, and the prompt parameter, must remain as stated. If you give this some thought, you can create a function that you can reuse in future programs.
Also write a dedicated function to get the supported operator from the user using the prototype
In your program,use an IF statement to determine which operator is being used for the table.
When the program completes displaying a table, prompt the user to see if they want to run again. Continue to iterate until the user signals they are done.
Your functions should display appropriate error messages if the values entered aren’t correct, and then keep running. For example:
Enter low row number: 1 Enter high row number: x
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int obtainUserIntegerInput( char *prompt );
char obtainOperatorInput( void );
void add(int lowRow, int highRow, int lowCol, int highCol)
{
int i,j;
printf(" + ");
for(i = lowCol; i <= highCol; i++)
printf("%4i",i);
printf(" ");
for(i = lowRow; i <= highRow; i++)
{
printf("%4i",i);
for(j = lowCol; j <= highCol; j++)
printf("%4i",i+j);
printf(" ");
}
}
void sub(int lowRow, int highRow, int lowCol, int highCol)
{
int i,j;
printf(" - ");
for(i = lowCol; i <= highCol; i++)
printf("%4i",i);
printf(" ");
for(i = lowRow; i <= highRow; i++)
{
printf("%4i",i);
for(j = lowCol; j <= highCol; j++)
printf("%4i",i-j);
printf(" ");
}
}
void mul(int lowRow, int highRow, int lowCol, int highCol)
{
int i,j;
printf(" * ");
for(i = lowCol; i <= highCol; i++)
printf("%4i",i);
printf(" ");
for(i = lowRow; i <= highRow; i++)
{
printf("%4i",i);
for(j = lowCol; j <= highCol; j++)
printf("%4i",i*j);
printf(" ");
}
}
void divide(int lowRow, int highRow, int lowCol, int highCol)
{
int i,j;
printf(" / ");
for(i = lowCol; i <= highCol; i++)
printf("%4i",i);
printf(" ");
for(i = lowRow; i <= highRow; i++)
{
printf("%4i",i);
for(j = lowCol; j <= highCol; j++)
printf("%4i",i/j);
printf(" ");
}
}
void mod(int lowRow, int highRow, int lowCol, int highCol)
{
int i,j;
printf(" %% ");
for(i = lowCol; i <= highCol; i++)
printf("%4i",i);
printf(" ");
for(i = lowRow; i <= highRow; i++)
{
printf("%4i",i);
for(j = lowCol; j <= highCol; j++)
printf("%4i",i%j);
printf(" ");
}
}
void power(int lowRow, int highRow, int lowCol, int highCol)
{
int i,j;
printf(" ^ ");
for(i = lowCol; i <= highCol; i++)
printf("%4i",i);
printf(" ");
for(i = lowRow; i <= highRow; i++)
{
printf("%4i",i);
for(j = lowCol; j <= highCol; j++)
printf("%4i",(int)pow(i,j));
printf(" ");
}
}
void root(int lowRow, int highRow, int lowCol, int highCol)
{
int i,j;
printf(" + ");
for(i = lowCol; i <= highCol; i++)
printf("%4i",i);
printf(" ");
for(i = lowRow; i <= highRow; i++)
{
printf("%4i",i);
for(j = lowCol; j <= highCol; j++)
printf("%4f",pow(i,1/j));
printf(" ");
}
}
void help()
{
printf(" Possible Operators: +: Addition. -: Subtraction. * or x or X: Multiplication. / orD: Division. %%: Modulus. p: Power r: Root h: Help. ");
}
int main()
{
int lowRow, highRow, lowCol,highCol,flag;
char op;
while(1)
{
lowRow = obtainUserIntegerInput("Enter low row number: ");
highRow = obtainUserIntegerInput("Enter high row number: ");
lowCol = obtainUserIntegerInput("Enter low column number: ");
highCol = obtainUserIntegerInput("Enter high column number: ");
printf("Enter operation: ");
op = obtainOperatorInput();
if(op == '+')
add(lowRow,highRow,lowCol,highCol);
else if(op == '-')
sub(lowRow,highRow,lowCol,highCol);
else if(op == '*' || op == 'X' || op == 'x')
mul(lowRow,highRow,lowCol,highCol);
else if(op == 'D' || op == '/')
divide(lowRow,highRow,lowCol,highCol);
else if(op == '%')
mod(lowRow,highRow,lowCol,highCol);
else if(op == 'p')
power(lowRow,highRow,lowCol,highCol);
else if(op == 'r')
root(lowRow,highRow,lowCol,highCol);
else if(op == 'h' || op == 'H')
help();
else
printf("Error: %c is not a valid operator.",op);
flag = obtainUserIntegerInput("Do you want to perform another operation(0: Exit): ");
if(flag == 0)
exit(1);
}
}
int obtainUserIntegerInput(char *prompt)
{
int number;
printf("%s",prompt);
scanf("%i",&number);
return number;
}
char obtainOperatorInput()
{
char op;
scanf(" %c",&op);
return op;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.