0

Problem1: When I tried outputting a printf "error message" at dividing by zero, it screwed up my code and no output was shown at all.

Problem2: When I perform an operation, I get 8 results instead of 5.

Please help ASAP. Thanks.

```
#include <stdio.h>
//Defining a few "functions" which will be used in the main function
#define MAX 5
float v1[MAX], v2[MAX], v3[MAX]; //First input vector, second input vector, and result vector respectively
//Defining "getVec" which will get each of 5 vectors that the user inputs
void getVec( float v[] ) {
int c, i;
for(i=0; i<MAX; ++i) {
printf( "Element %d: ", i+1 );
scanf( "%f", &v[i] );
while( (c= getchar()) !='\n' ) {
c= getchar(); /* 'flush' stdin */
}
}
}
//In turn, showVec will output the vectors typed or calculated
void showVec( float v[] ) {
int i;
for(i=0; i<MAX-1; ++i) {
printf( "%.2f ", v[i] );
printf( "%.2f ", v[MAX-1] );
}
}
//Defining the addition process for all 5 vector components
void add() {
int i;
for(i=0; i<MAX; ++i) {
v3[i] = v1[i] + v2[i];
}
}
//Defining the subtraction for all 5 vector components
void sub() {
int i;
for(i=0; i<MAX; ++i) {
v3[i] = v1[i] - v2[i];
}
}
//Defining the multiplication for all 5 vector components
void mult() {
int i;
for(i=0; i<MAX; ++i) {
v3[i] = v1[i] * v2[i];
}
}
//Defining the division for all 5 vector components
void divide() {
int i;
for(i=0; i<MAX; ++i) {
v3[i] = v1[i] / v2[i];
}
}
//My main function, user interaction, and "stupid-proofing"
int main() {
int c;
int vecFlag= 1; //if "vecFlag" is True, calculator starts and keeps looping
//If False -0-, it stops
char operation;
//Infinite loop until "return 0"
for(;;) {
if(vecFlag) {
printf("\nGetting 1st vector of %d elements ...\n", MAX);
getVec( v1 ); /* vector is passed in and returned by 'reference' */
printf("\nGetting 2nd vector of %d elements ...\n", MAX);
getVec( v2 );
}
//User chooses the operation sign
printf( "Enter an operation from this list + - * / Your choice : " );
scanf( "%c", &operation );
c= operation;
while( c!='\n' ) {
c= getchar(); /* 'flush' stdin ... */
}
//If the user chooses +, addition is done using the "add" function I defined before
if( operation == '+' ) {
add();
printf("\nShowing sum vector.\n");
showVec( v3 );
}
//If the user chooses -, subtraction is done using the "sub" function I defined before
else if( operation == '-' ) {
sub();
printf("\nShowing difference vector.\n");
showVec( v3 );
}
//If the user chooses *, multiplication is done using the "mult" function I defined before
else if( operation == '*' ) {
mult();
printf("\nShowing product vector.\n");
showVec( v3 );
}
//If the user chooses /, division is done using the "divide" function I defined before
else if( operation == '/' ) {
divide();
printf("\nShowing division result - remember: you cannot divide by zero.\n");
showVec( v3 );
}
//Anything, other than the 4 signs, will cause an error
else printf( "\nInput Error! Operation must be one of + - * / \n" );
//User decides whether they want to do more calculations
printf( "\nContinue? (y/n)" );
c= getchar();
if( c=='n' || c=='N' ) {
return 0;
}
while( c!='\n' ) {
c= getchar(); /* 'flush' stdin ... */
vecFlag= 0;
}
//User decides if the vectors for the new calculation are new ones
printf( "\nNew vectors? (y/n)" );
c= getchar();
if( c=='y' || c=='Y' ) {
vecFlag= 1;
}
while( c!='\n' ) {
c= getchar(); /* 'flush' stdin ... */
}
} //Closing "for" loop
}
//End of program
```