Codeblocks (C code) Write a program that prompts the user with the following mai
ID: 3667826 • Letter: C
Question
Codeblocks (C code)
Write a program that prompts the user with the following main menu that prompts the user for an input. The program should check that the user input is valid and should prompt the user again otherwise:
E – Enter a Value
D – Dot product
C – Cross product
N – Vector Norm
A – Sum
M – Scalar Multiply
P – Print Value
Q – Terminate Program
If the user enters E from the main menu, the following prompt will be given:
1 – Enter a value for vector 1
2 – Enter a value for vector 2
3 – Enter a value for vector 3
a – Enter a value for scalar a
b – Enter a value for scalar b
c – Enter a value for scalar c
If the user enters 1, 2 or 3, the following prompt will be given:
Enter the x-,y-,and z-coordinates separated by spaces for vector .
If the user enters a, b or c, the following prompt will be given:
Enter the value of scalar .
This operation will check for valid inputs. At the conclusion of this operation, the program will return to the main menu.
If the user enters D, the following prompt will be given:
Dot Product:
1 – Enter a value for vector 1
2 – Enter a value for vector 2
3 – Enter a value for vector 3
a – Enter a value for scalar a
b – Enter a value for scalar b
c – Enter a value for scalar c
Enter first argument, second argument, and where the resultant will be stored separated by spaces:
So the user would enter “2 3 a” to take the dot product of vector 2 and vector 3 and store the result to scalar a. The operation will check for valid inputs. Both arguments must be vectors and the result must be a scalar. At the conclusion of this operation, the program will return to the main menu.
If the user enters C, the following prompt will be given:
Cross Product:
1 – Enter a value for vector 1
2 – Enter a value for vector 2
3 – Enter a value for vector 3
a – Enter a value for scalar a
b – Enter a value for scalar b
c – Enter a value for scalar c
Enter first argument, second argument, and where the resultant will be stored separated by spaces:
So the user would enter “2 3 1” to take the cross product of vector 2 and vector 3 and store the result to vector 1. The operation will check for valid inputs. The arguments must be vectors and result must be a vector. At the conclusion of this operation, the program will return to the main menu.
If the user enters N, the following prompt will be given:
Norm:
1 – Enter a value for vector 1
2 – Enter a value for vector 2
3 – Enter a value for vector 3
a – Enter a value for scalar a
b – Enter a value for scalar b
c – Enter a value for scalar c
Enter argument, and where the resultant will be stored separated by spaces:
So the user would enter “2 a” to take the square root of the dot project of vector 2 with vector 2 and store the result to scalar a. The operation will check for valid inputs. The argument must be a vector and the result must be a scalar. At the conclusion of this operation, the program will return to the main menu.
If the user enters A, the following prompt will be given:
Sum:
1 – Enter a value for vector 1
2 – Enter a value for vector 2
3 – Enter a value for vector 3
a – Enter a value for scalar a
b – Enter a value for scalar b
c – Enter a value for scalar c
Enter first argument, second argument, and where the resultant will be stored separated by spaces:
So the user would enter “2 3 2” to add the vector two to the vector 3 and store the result to vector 2. The user issues “a 2 2” to add a to each element of 2 and store the result to vector 2. The user will enter “a b c” to add the scalar a to the scalar b and store the result to scalar c. The operation will check for valid inputs. At the conclusion of this operation, the program will return to the main menu.
If the user enters M, the following prompt will be given:
Sum:
1 – Enter a value for vector 1
2 – Enter a value for vector 2
3 – Enter a value for vector 3
a – Enter a value for scalar a
b – Enter a value for scalar b
c – Enter a value for scalar c
Enter first argument, second argument, and where the resultant will be stored separated by spaces:
So the user would enter “a 1 2” to multiply each element of vector1 by scalar a and store the result to vector 2. The user will enter “a b c” to multiply the scalar a to the scalar b and store the result to scalar c. The operation will check for valid inputs. At the conclusion of this operation, the program will return to the main menu.
If the user enters P, the following prompt will be given:
Sum:
1 – Enter a value for vector 1
2 – Enter a value for vector 2
3 – Enter a value for vector 3
a – Enter a value for scalar a
b – Enter a value for scalar b
c – Enter a value for scalar c
Enter value to be printed.
Vectors are to be printed in the following format:
2.15x + -1.74y +5.57z
The operation will check for valid inputs. At the conclusion of this operation, the program will return to the main menu.
If the user enters Q, immediately terminate the program.
Rules:
All data types vectors and scalars should be of type float.
Vectors contain three fields representing and x, y, and z value
The value of each of the vectors and scalars will be stored until program termination.
The initial value of each scalar and vector will be 0.
If you don’t know what dot products, cross product, Euclidean norm, or other operations do, please look them up.
NO FUNCTION SHALL HAVE MORE THAN 15 LINES OF CODE
Explanation / Answer
Based on Question It is in developed in Switch Case Program
/*
* File: SwitchInC.c
* Author: HARE KRISHNA
*
* Created on 14 February, 2016, 11:52 AM
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main() {
// your code goes here
printf("Enter your Choice like E,D,C,N,A,M,P,Q" );
printf("E - Enter a Value ");
printf("D - Dot Product ");
printf("C - Cross Product ");
printf("N - Vector Norm ");
printf("A - Sum ");
printf("M - Scalar Multiply ");
printf("P - Print Value ");
printf("Q - Terminate Program ");
char choice;// here you can send like E,D,C,N,A,M,P,Q
Scanf("%c",choice);
char Echoice,Dchoice,Cchoice,Nchoice,SumChoice,Mchoice,Pchoice;
int x,y,z;
int c;
switch(choice){
case 'E':
printf("Enter value of E choice ");
printf("1- Enter a Value for Vector 1 ");
printf("2 - Enter a Value for Vector 2 ");
printf("3 - Enter a Value for Vector 3 ");
printf("a - Enter a Value for scalar a ");
printf("b - Enter a Value for scalar b ");
printf("c - Enter a Value for scalar c ");
scanf("%c",Echoice);
if (Echoice =='1' || Echoice =='2'|| Echoice=='3'){
printf("Enter the co-ordinate values of x,y,z vector");
scanf("%d%d%d",&x,&y,&z);
}
else if (Echoice =='a' || Echoice =='b'|| Echoice=='c'){
printf("Enter the scalar values of a,b,c");
scanf("%d%d%d",&a,&b,&c);
}
break;
case 'D':
printf("Enter value of Dot product choice ");
printf("1- Enter a Value for Vector 1 ");
printf("2 - Enter a Value for Vector 2 ");
printf("3 - Enter a Value for Vector 3 ");
printf("a - Enter a Value for scalar a ");
printf("b - Enter a Value for scalar b ");
printf("c - Enter a Value for scalar c ");
scanf("%c",Dchoice);
if (Dchoice =='2' && Dchoice =='3' && Dchoice =='a'){
a = x+y;// vector x and vector y in add place into scalar a
}
break;
case 'C':
printf("Enter value of Cross product choice ");
printf("1- Enter a Value for Vector 1 ");
printf("2 - Enter a Value for Vector 2 ");
printf("3 - Enter a Value for Vector 3 ");
printf("a - Enter a Value for scalar a ");
printf("b - Enter a Value for scalar b ");
printf("c - Enter a Value for scalar c ");
scanf("%c",Cchoice);
if (Dchoice =='2' && Dchoice =='3' && Dchoice =='1'){
x = y*z;// vector z and vector y in product in x
}
break;
case 'N':
printf("Enter value of Norm product choice ");
printf("1- Enter a Value for Vector 1 ");
printf("2 - Enter a Value for Vector 2 ");
printf("3 - Enter a Value for Vector 3 ");
printf("a - Enter a Value for scalar a ");
printf("b - Enter a Value for scalar b ");
printf("c - Enter a Value for scalar c ");
scanf("%c",Nchoice);
if (Nchoice =='2' && Nchoice =='3' && Nchoice =='2'){
y = y+z;// vector y and vector z in add and placed in y
}
if (Nchoice =='a' && Nchoice =='2' && Nchoice =='2'){
y = a+y;
}
if (Nchoice =='a' && Nchoice =='b' && Nchoice =='c'){
c = a+b;// scalar a and scalar b in add and placed in c
}
return c;
break;
case 'A':
printf("Enter value of Norm product choice ");
printf("1- Enter a Value for Vector 1 ");
printf("2 - Enter a Value for Vector 2 ");
printf("3 - Enter a Value for Vector 3 ");
printf("a - Enter a Value for scalar a ");
printf("b - Enter a Value for scalar b ");
printf("c - Enter a Value for scalar c ");
scanf("%c",Sumchoice);
if (Nchoice =='1' && Nchoice =='2'){
c = x+y;// add two values separtdly placed in c
}
return c;
break;
case 'M':
printf("Enter value of Norm product choice ");
printf("1- Enter a Value for Vector 1 ");
printf("2 - Enter a Value for Vector 2 ");
printf("3 - Enter a Value for Vector 3 ");
printf("a - Enter a Value for scalar a ");
printf("b - Enter a Value for scalar b ");
printf("c - Enter a Value for scalar c ");
scanf("%c",Mchoice);
if (Mchoice =='a' && Mchoice =='1' && Mchoice=='2'){
y=a*x;
}
if (Mchoice =='a' && Mchoice =='b' && Mchoice=='c'){
c=a*b;
}
break;
case 'P':
printf("Enter value of Norm product choice ");
printf("1- Enter a Value for Vector 1 ");
printf("2 - Enter a Value for Vector 2 ");
printf("3 - Enter a Value for Vector 3 ");
printf("a - Enter a Value for scalar a ");
printf("b - Enter a Value for scalar b ");
printf("c - Enter a Value for scalar c ");
scanf("%c",Pchoice);
if(Pchoice){
printf("%c%c%c",x*a,"+",y*b,"+",z*c);// format
}
break;
case 'Q':
Printf("Quit the Program");
exit(0);
}
return 0;
}