I am trying to code two programs for uni. I am almost there, I can feel it! But not quite.
For starters; this code here, I'm trying to run but I'm not too sure on how to fix the errors The errors state Expected '(' before 'radius' (line 101:9) Expected 'while' before 'case' case 2: (line 129:2 Expected 'while' before 'default' (line 185:5)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
#define PI 3.14159
//DIAMETER TO RADIUS
double diameter_to_radius(double radius)
{
return radius * 2;
}
//RANDOM VALUE GENERATOR
double random_value_generator(int Rand_MIN, int Rand_MAX)
{
double Rand_Value;
double range = (Rand_MAX - Rand_MIN);
double div = range / Rand_MAX;
Rand_Value = Rand_MIN + (rand() * div);
return Rand_Value;
}
//AREA CALCULATION
double area(double radius, double diameter)
{
if (radius < diameter)
{
return PI*(radius* radius);
}
else
{
return PI*diameter;
}
}
//CIRCUMFEREnCE CALCULATION
double circumference(double radius, double diameter)
{
if (radius < diameter)
{
return 2 * PI * radius;
}
else
{
return PI*diameter;
}
}
int main()
{
double diameter;
double radius;
int NumCode;
int NumCode2;
int NumCode3;
char Unit;
char Calculate;
double Rand_Value;
// Is a Menu for usability
do {
printf( "1. Enter 1 for RADIUS \n" );
printf( "2. Enter 2 for DIAMETER \n" );
printf( "-1. Exit \n" );
printf( "Code Selected: " );
scanf( "%d", &NumCode );
switch (NumCode)
{
case 1:
do {
printf( "1. Enter 1 for CM \n" );
printf( "2. Enter 2 for M \n" );
printf( "-1. Exit \n" );
printf( "Code Selected: " );
scanf( "%d", &NumCode3 );
switch(NumCode3)
{
case 1:
printf("You have selected CM \n");
scanf("%s", Unit);
break;
case 2:
printf("You have selected M\n");
break;
default:
printf( "You have entered an invalid choice\n" );
break;
}
}
while radius = random_value_generator(0,15); // for the radius
printf("The radius given by random generator is: %2.2f %s\n",Rand_Value,Unit);
do {
printf( "1. Enter 1 for AREA \n" );
printf( "2. Enter 2 for CIRCUMFERENCE \n" );
printf( "-1. Exit \n" );
printf( "Code Selected: " );
scanf( "%d", &NumCode2 );
switch(NumCode2)
{
case 1:
diameter = 0;
printf("Area of circle with radius of %f is %5.2f %s\n", radius,area(radius,diameter),Unit);
break;
case 2:
printf("Circumference of circle with radius of %2.2f is %2.2f %s\n",radius,circumference(radius,diameter),Unit);
break;
default:
printf( "You have entered an invalid choice\n" );
break;
}
} //Code stops
case 2:
do {
printf( "1. Enter 1 for CM \n" );
printf( "2. Enter 2 for M \n" );
printf( "-1. Exit \n" );
printf( "Code Selected: " );
scanf( "%d", &NumCode3 );
switch(Measurement)
{
case 1:
printf("You have selected CM \n");
scanf("%s", Unit);
break;
case 2:
printf("You have selected M\n");
break;
default:
printf( "You have entered an invalid choice\n" );
break;
}
}
radius = random_value_generator(0,15); // for the diameter
printf("The diameter given by random generator is: %2.2f %s\n",diameter_to_radius(radius),Unit);
printf("The corresponding radius is: %2.2f %s\n",radius,Unit);
do {
printf( "1. Enter 1 for AREA \n" );
printf( "2. Enter 2 for CIRCUMFERENCE \n" );
printf( "-1. Exit \n" );
printf( "Code Selected: " );
scanf( "%d", &NumCode2 );
switch(Calculate)
{
case 1:
diameter = 0;
printf("Area of circle with diameter of %f is %5.2f %s\n",diameter_to_radius(radius),area(radius,diameter),Unit);
break;
case 2:
printf("Circumference of circle with diameter of %2.2f is %2.2f %s\n", diameter_to_radius(radius),circumference(radius,diameter),Unit);
break;
default:
printf( "You have entered an invalid choice\n" );
break;
}
}
default:
printf( "You have entered an invalid choice\n" );
break;
}
}while(NumCode!=-1);
return 0;
}
Secondly; My second program works... Not really. The values it outputs are redicously high and not what its meant to do. Im not sure what I should do to fix it nor the problem. Im meant to: Write a complete C program to do the following tasks: (i) Read a positive integer from user as the value of m (need to verify that m is positive but less than 30). (ii) Read in the m elements (up to 30) of a one-dimensional floating point array, compute the sum of these elements, the mean, the deviation, and the standard deviation.
This is the code I've done so far.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main()
{
int i;
i = 0;
float mean;
float deviation;
float standard_deviation;
int integer;
int z;
printf("Input a positive integer: ");
scanf("%d",&integer);
int x[integer];
while(i<1)
{
if((integer > 0) && (integer < 30))
{
for(z=0; z<integer;z++)
{
srand(time(NULL));
printf("x[%d]=%d\n",z,x);
mean = mean + x[z];
}
mean = mean/integer;
printf("Mean is %.2f\n",mean);
for(z = 0; z < integer; z++)
{
deviation = x[z] - mean;
printf("Deviation is [x = %.2f] = %.2f\n",z,deviation);
}
printf("Standard Deviation = %.2f\n", standard_deviation);
i++;
}
else
{
printf("Wrong input, please try again \n");
printf("Input a positive integer: ");
scanf("%d\n",&integer);
}
}
return 0;
}
Would be good if you guys couyld help me! TRhanks
Aucun commentaire:
Enregistrer un commentaire