CODE
#include <stdio.h>
#include <math.h>
/* function prototypes */
void prob1(void);
void prob2(void);
void prob3(void);
/* add here additional function prototypes as needed */
main()
{
int menu;
printf("Enter the function number to execute (1-3):");
scanf("%d", &menu);
switch(menu){
case 1:
prob1();
break;
case 2:
prob2();
break;
case 3:
prob3();
break;
default:
printf("prob%d() does not exist.\n", menu);
}
exit(0);
}
void prob1(void)
{
//printf(" Put your solution for problem 1 here:\n");
double a_1, b_1, c_1, a_2, b_2, c_2;
double D, D_1, D_2, x_1, x_2, y, y_1;
printf("\nEnter a_1, b_1, c_1\n");
scanf("%lf %lf %lf", &a_1, &b_1, &c_1);
printf("\n\nEnter a_2, b_2, c_2\n");
scanf("%lf %lf %lf", &a_2, &b_2, &c_2);
printf("\n\n");
D=a_1*b_2-b_1*a_2;
D_1=c_1*b_2-b_1*c_2;
D_2=a_1*c_2-c_1*a_2;
x_1=D_1/D;
x_2=D_2/D;
c_1=a_1*x_1+b_1*x_2;
c_2=a_2*x_1+b_2*x_2;
y=sqrt(pow(x_1, 2)+pow(x_2, 2));
y_1=log10(y);
printf("x_1 = %f, x_2 = %f", x_1, x_2);
if(D=0) printf("The equations are linearly dependent\n");
else printf("\n\nThe value of y is %f\n", y_1);
}
void prob2(void)
{
//printf(" Put your solution for problem 2 here:\n");
int num, row, num2, *v;
double num1, sum;
char text[80];
FILE *infile, *outfile;
infile = fopen("data.dat", "r");
outfile = fopen("output2.dat", "w");
if (infile == NULL)
{
printf(" Error Opening file!");
exit(0);
}
fscanf(infile, "%d", &num);
// printf("%d\n", num);
v = (int *)calloc((size_t)num, sizeof(int *));
if( v == NULL)
{
printf("error");
exit (0);
}
printf("v[] = {");
for(num2=0; num2<num; num2++)
{
fscanf(infile, "%lf", &v[num2]);
printf("%.2f ", v[num2]);
// sum += v[num2] * v[num2];
}
sum = 0;
for(num2=0; num2<num; num2++)
{ sum = sum + pow(v[num2], 2);
}
printf("}\n\n");
printf("\nsum = %f\n", sum);
sum = sqrt(sum);
printf("The length of the vector v[] is %f\n", sum);
}
void prob3(void)
{
printf(" Put your solution for problem 3 here:\n");
}
Hi, 2 questions
I am trying to run prob2. However, when i try and calculate the length of the array, it turns out to be a number that is like 15 digits long when the length should relaly be 8.42585. Not sure why it isn't working out.
Also, the second part or prob2 which i have yet to write involves rearranging those 10 numbers in ascending order, how do i write a program that will arrange it for me?
Thank you!