1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
#define PI = 3.14159265
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI = 3.14159265
int main ()
{
int option;
char repeat;
system("CLS");
do
{
printf("*************************************\n");
printf("(1) Addition\n");
printf("(2) Subtraction\n");
printf("(3) Multiplication\n");
printf("(4) Division\n");
printf("(5) Optional function 1 (please name it)\n");
printf("(6) Optional function 2 (please name it)\n");
printf("(7) Optional function 3 (please name it)\n");
printf("(8) Optional function 4 (please name it)\n");
printf("(9) Quit\n");
printf("*************************************\n");
printf("Please Input Your Choice:");
scanf("%d",&option);
system ("cls");
if(option ==1)
{
// Numbers: ...... Contains the allocated resources.
// BiggestNum: ... Holds the biggest number within 'Numbers'.
// InputBuffer: .. Contains the value given by the user.
// Total: ........ Contains the total of all numbers within 'Numbers'.
// I: ............ Used within the 'for' loop as the counter.
// Average: ...... Holds the average of 'Total'.
float *Numbers = NULL, BiggestNum = 0,Total = 0;
int InputBuffer = 0;
int I = 0;
// Request the user input.
do
{
// Inform the user to input a value (make sure they know what the value
// they're entering is for). If the given value is less than or equal
// to zero, or, greater than 80, make then enter another number.
// Otherwise, break from the loop.
printf( "Please Enter How Many Number You Want to Add(1~80): " );
scanf( "%d", &InputBuffer );
} while( ( InputBuffer < 1 ) || ( InputBuffer > 80 ) );
// Dynamically create the array based on the user input (InputBuffer).
Numbers = ( ( float * )calloc( InputBuffer, sizeof( int ) ) );
if( Numbers == NULL )
{
// The allocation of 'Numbers' failed. Inform the user that the program
// will end.
printf( "Failed to allocate the array. Exiting" );
getchar( );
return 1;
}
{
// Here, we'll enter a loop which will perform 'n' passes. The amount of
// times passes is determined by the value within 'InputBuffer'.
for( I = 0; I < InputBuffer; I++ )
{
// We'll use 'BiggestNum' here for two reasons:
// 1) To save stack space.
// 2) 'BiggestNum' isn't used until later on.
printf( "Enter number[%d]: ", ( I + 1 ) );
scanf( "%f", &BiggestNum );
Numbers[I] = BiggestNum;
}
// Clear 'BiggestNum' from the last operation.
BiggestNum = 0;
// Here, we'll find the biggest number and store it within 'BiggestNum'.
for( I = 0; I < InputBuffer; I++ )
if( BiggestNum < Numbers[I] )
BiggestNum = Numbers[I];
// We'll enter another loop here. This time, we'll add up all if the numbers
// within 'Numbers'. The result of the computation will be stored within the
// previously defined variable, 'Total'.
for( I = 0; I < InputBuffer; I++ )
Total += Numbers[I];
// Release the allocated memory. if we don't we'll have leaking memory.
// Print the value stored within 'Total'.
printf( "\nSum of the numbers entered is: %f\n", Total );
}
}
else
{
if(option == 2)
{
printf("Not done");
}
else
{
if(option == 3)
{
// Numbers: ...... Contains the allocated resources.
// BiggestNum: ... Holds the biggest number within 'Numbers'.
// InputBuffer: .. Contains the value given by the user.
// Total: ........ Contains the total of all numbers within 'Numbers'.
// I: ............ Used within the 'for' loop as the counter.
// Average: ...... Holds the average of 'Total'.
int *Numbers1 = NULL, BiggestNum1 = 0;
int InputBuffer1 = 0;
long Total1 = 1;
int I1 = 0;
// Request the user input.
do
{
// Inform the user to input a value (make sure they know what the value
// they're entering is for). If the given value is less than or equal
// to zero, or, greater than 80, make then enter another number.
// Otherwise, break from the loop.
printf( "Please Enter How Many Number You Want to Add(1~80): " );
scanf( "%d", &InputBuffer1 );
} while( ( InputBuffer1 < 1 ) || ( InputBuffer1 > 80 ) );
// Dynamically create the array based on the user input (InputBuffer).
Numbers1 = ( ( int * )calloc( InputBuffer1, sizeof( int ) ) );
if( Numbers1 == NULL )
{
// The allocation of 'Numbers' failed. Inform the user that the program
// will end.
printf( "Failed to allocate the array. Exiting" );
getchar( );
return 1;
}
{
// Here, we'll enter a loop which will perform 'n' passes. The amount of
// times passes is determined by the value within 'InputBuffer'.
for( I1 = 0; I1 < InputBuffer1; I1++ )
{
// We'll use 'BiggestNum' here for two reasons:
// 1) To save stack space.
// 2) 'BiggestNum' isn't used until later on.
printf( "Enter number[%d]: ", ( I1 + 1 ) );
scanf( "%d", &BiggestNum1 );
Numbers1[I1] = BiggestNum1;
}
// Clear 'BiggestNum' from the last operation.
BiggestNum1 = 0;
// Here, we'll find the biggest number and store it within 'BiggestNum'.
for( I1 = 0; I1 < InputBuffer1; I1++ )
if( BiggestNum1 < Numbers1[I1] )
BiggestNum1 = Numbers1[I1];
// We'll enter another loop here. This time, we'll add up all if the numbers
// within 'Numbers'. The result of the computation will be stored within the
// previously defined variable, 'Total'.
for( I1 = 0; I1 < InputBuffer1; I1++ )
Total1 *= Numbers1[I1];
// Release the allocated memory. if we don't we'll have leaking memory.
// Print the value stored within 'Total'.
printf( "\nMutiplication of the numbers entered is: %d\n", Total1 );
}
}
else
if(option == 4)
{
printf("Not done ");
}
else
if(option ==5)
{
float param, x ;/* declare the value*/
float sine(float );//Delaring function
printf( "Enter degree(x)");// ask user to enter the value
scanf("%f",¶m);// get the value
x = sin( param*PI / 180);// calculation
printf("x = %f",x);// the answer
}
float x ;
float sine(float param)//@ here when i want too call the function , it force me to put a ;
{
return sin( x );// return statement
}
}
}
printf( "For the program to repeat please input Y \n");
fflush(stdin);//add code;
scanf("%c",&repeat);//true code;
}while(repeat == 'y'|| repeat == 'Y');
}
[For the Line After Option==5, i Realise That There are Many Errors But When i Do without [option ==1 - option == 4] i was able to do it.
[This is the program that was able to run when it was not connescted to a loop]
#define PI 3.14159265
void main ()
{
float param, x ;//declare the value
float sine(float );//declaring a function
printf( "Enter degree(x)");//ask user to enter
scanf("%f",¶m);// get the value
x =sin( param*PI/180);//call function sin to calculate
printf("x = %f",x);
}
float x;
float sine(float param)//calling function
{
return sin( x );
}
|