Putting Function In To A Program

#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)

int main (void);

{
float param, x ;

float sine(float );
printf( "Enter degree(x)");
scanf("%f",&param);

x = sin( param*PI / 180);
printf("x = %f",x);
}
float x ;
float sine(float param);
{
return sin( x );
}
}
}
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.#define PI 3.14159265
void main ()
{
float param, x ;
float sine(float );
printf( "Enter degree(x)");
scanf("%f",&param);

x =sin( param*PI/180);
printf("x = %f",x);
}
float x;
float sine(float param)
{
return sin( x );
}
Code tags help your code be more readable. And a more clearly defined question will get a more clear answer. For now just read this: http://cplusplus.com/forum/beginner/1/
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",&param);// 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",&param);// 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 );
} 
#define PI = 3.14159265

This will replace "PI" with "= 3.14159265"

For example, the line

x =sin( param*PI/180);//call function sin to calculate

becomes

x =sin( param*= 3.14159265 /180);//call function sin to calculate

Is that what you wanted? I suspect not.

#define is an instruction to the preprocessor to go through your text and replace some with something else, character for character.
hm... line 195 there is a problem i am suppose to call the function out but the programm which is the c++ said i lack of a ; that is the problem that i am facing.
just some question, does it matter if i change PI like you to
x =sin( param*= 3.14159265 /180);//call function sin to calculate
http://cplusplus.com/doc/tutorial/functions/
That link should help you with writing functions in a program.
I did some major makeovers to your code, and finally came up with this:
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265

float sine(float );//Delaring function prototype

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*/
            printf( "Enter degree(x)");// ask user to enter the value
            scanf("%f",&param);// get the value

            x = sin( param*PI / 180);// calculation
            printf("x = %f",x);// the answer 
        }
        printf( "For the program to repeat please input Y \n");
        fflush(stdin);//add code;
        scanf("%c",&repeat);//true code;
    } while(repeat == 'y'|| repeat == 'Y');
}

float sine(float param)//defining function code
{
    return sin( param );
}
may i know where is wrong? as i have 3 more function to add:P
closed account (zb0S216C)
kevinkjt2000, after altering my original code, you've managed to create a memory leak. Why did you remove free( )?

Wazzak
Framework is that for me :O?
closed account (zb0S216C)
Frozendog, if you did remove it, why? free( ) is required to release the resources allocated calloc( ). Since you've removed it, you get a memory leak every time your execute the program.

Wazzak
Memory Leaks are a no-no
They take up more memory from the heap memory and if you use to much without freeing it it stays there and it can make your computer slow after time and prevent you from allocating more memory. BTW I have been wondering what is the difference between the STACK memory and heap? I have heard or programmers talk about both.
closed account (zb0S216C)
A stack is a 1 megabyte block of memory that stores non-dynamically allocated variables/objects. The system memory, in some cases, is referred to as the heap. This is where dynamically allocated memory is taken from.

Wazzak
Oh so there is no difference?
closed account (zb0S216C)
There's a performance difference. The stack is considered faster than the heap. However, on a side note, the default stack size can be altered, and according to Microsoft (MSDN), the stack size of a program can also be altered[1].

References:
[1]http://msdn.microsoft.com/en-us/library/tdkhxaks(v=vs.71).aspx


Wazzak
I didn't even notice that memory leak error in the code. Good catch Framework. It is important to free memory in programs.
Thanks for all ur help and i added the free () into it ..
Topic archived. No new replies allowed.