### Thread: Need code check help

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
7
Rep Power
0

#### Need code check help

The task is - Argument initial value A, step H and the number of steps N. Calculate the value of the function in A, A + H, A + 2*H, … , A + N*H.

and function itself is prntscr.com/p381j
(new members are not allowed to add url or pics)

I have write my code into example task code and theree are some errors. Dont beat me please for this ugly code and lets learn together
Code:
```#include <stdio.h>
#include <math.h>

void sisestamine (int A, int H, int N)
{
printf("Sisesta funktsiooni argumendi Algväärtus A\n");
scanf("%d", A);
printf("Sisesta sammu v22rtus H\n");
scanf("%d", H);
printf("Sisesta sammude arv N\n");
scanf("%d", N);
}

void arvutamine (int A, int H, int N, int x, float y)
{
int i=0;
int x=A;
do
{
y[i] = ((pow(sin.2)(x[i]+5))+(cos(x[i])))/(x[i]+(pow(2,72.(x[i]+3))));
A=A*H
i++;
}
while (i<N);
}
void v2ljastus (int x, float y)
{
int i;
printf("X    |    Y\n");
for(i=0;i<=N;i++)
{
printf("%d   |   %f\n", x[i], y[i]);
}
}
int main(void)
{
int i;
int x;
float y;
int A, H, N;
sisestamine (&A,&H,&N);
arvutamine (A,H,N,x,y);
v2ljastus (x,y);
printf("Vajuta enter v2ljumiseks!\n");
getchar();
return 0;
}```
2. First, you need to learn how to indent code.
As well as making it easier for others to help you, it will save you from making many simple mistakes.

Code:
```#include <stdio.h>
#include <math.h>

void sisestamine(int A, int H, int N)
{
printf("Sisesta funktsiooni argumendi Algväärtus A\n");
scanf("%d", A);
printf("Sisesta sammu v22rtus H\n");
scanf("%d", H);
printf("Sisesta sammude arv N\n");
scanf("%d", N);
}

void arvutamine(int A, int H, int N, int x, float y)
{
int i = 0;
int x = A;
do {
y[i] = ((pow(sin .2) (x[i] + 5)) + (cos(x[i]))) / (x[i] + (pow(2, 72.(x[i] + 3))));
A = A * H i++;
}
while (i < N);
}

void v2ljastus(int x, float y)
{
int i;
printf("X    |    Y\n");
for (i = 0; i <= N; i++) {
printf("%d   |   %f\n", x[i], y[i]);
}
}

int main(void)
{
int i;
int x;
float y;
int A, H, N;
sisestamine(&A, &H, &N);
arvutamine(A, H, N, x, y);
v2ljastus(x, y);
printf("Vajuta enter v2ljumiseks!\n");
getchar();
return 0;
}```
The next thing to do is get a decent compiler.
Code:
```\$ gcc -Wall -Wextra foo.c
foo.c: In function ‘sisestamine’:
foo.c:7:3: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]
foo.c:9:3: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]
foo.c:11:3: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]
foo.c: In function ‘arvutamine’:
foo.c:18:7: error: ‘x’ redeclared as different kind of symbol
foo.c:15:42: note: previous definition of ‘x’ was here
foo.c:20:6: error: subscripted value is neither array nor pointer nor vector
foo.c:20:22: error: expected ‘)’ before numeric constant
foo.c:20:22: error: incompatible type for argument 1 of ‘pow’
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:154:1: note: expected ‘double’ but argument is of type ‘__attribute__((const)) double (*)(double)’
foo.c:20:22: error: too few arguments to function ‘pow’
foo.c:20:28: error: subscripted value is neither array nor pointer nor vector
foo.c:20:46: error: subscripted value is neither array nor pointer nor vector
foo.c:20:57: error: subscripted value is neither array nor pointer nor vector
foo.c:20:76: error: subscripted value is neither array nor pointer nor vector
foo.c:20:74: error: called object ‘7.2e+1’ is not a function
foo.c:21:15: error: expected ‘;’ before ‘i’
foo.c:18:7: warning: variable ‘x’ set but not used [-Wunused-but-set-variable]
foo.c:15:51: warning: parameter ‘y’ set but not used [-Wunused-but-set-parameter]
foo.c: In function ‘v2ljastus’:
foo.c:30:20: error: ‘N’ undeclared (first use in this function)
foo.c:30:20: note: each undeclared identifier is reported only once for each function it appears in
foo.c:31:30: error: subscripted value is neither array nor pointer nor vector
foo.c:31:36: error: subscripted value is neither array nor pointer nor vector
foo.c:26:20: warning: parameter ‘x’ set but not used [-Wunused-but-set-parameter]
foo.c:26:29: warning: parameter ‘y’ set but not used [-Wunused-but-set-parameter]
foo.c: In function ‘main’:
foo.c:41:3: warning: passing argument 1 of ‘sisestamine’ makes integer from pointer without a cast [enabled by default]
foo.c:4:6: note: expected ‘int’ but argument is of type ‘int *’
foo.c:41:3: warning: passing argument 2 of ‘sisestamine’ makes integer from pointer without a cast [enabled by default]
foo.c:4:6: note: expected ‘int’ but argument is of type ‘int *’
foo.c:41:3: warning: passing argument 3 of ‘sisestamine’ makes integer from pointer without a cast [enabled by default]
foo.c:4:6: note: expected ‘int’ but argument is of type ‘int *’
foo.c:37:7: warning: unused variable ‘i’ [-Wunused-variable]```
One of the really neat things about gcc is that it can tell you when you mess up printf/scanf calls (and you have many of these).

The first problem you have is that sisestamine() is just not returning results back to main.
3. If you write this in TeX format or in a way that wolfram alpha would understand I'll help you write it so that c might parse it.

y[i] = ((pow(sin .2) (x[i] + 5)) + (cos(x[i]))) / (x[i] + (pow(2, 72.(x[i] + 3))));

Difficult because x is declared twice but never as an array.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
7
Rep Power
0
Sorry but i have on 16.01 one more examination, after this i can again continue with the code but the function looks like this :
5. Code:
```X = x[i];
y[i] = (pow(sin(X+5),2) + cos(X)) / (X + exp(X+3));```
(of course you'll need to declare X)
Last edited by b49P23TIvg; January 15th, 2013 at 07:32 PM.
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
7
Rep Power
0
impossible that its so easy :eek: , but i will check tomorrow after examination. Thanks a lot