# Thread: I'm a beginner. HELP!

1. ## I'm a beginner. HELP!

Could you please tell me what is wrong with my code because the results i take after i compile it are wrong

ps.This is my first programm in C

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

int main(){
float V, Imax, Imin;
int n, i;
printf("Dwste ton arithmo twn antistasewn\n");
scanf("%d", &n);
printf("Dwste thn timh ths tashs V\n");
scanf("%f", &V);
printf("Dwste thn megisth timh ths entashs tou reumatos\n");
scanf("%f", &Imax);
printf("Dwste thn elaxisth timh ths entashs tou reumatos\n");
scanf("%f", &Imin);
float Rmax=0., Rmin, Srow=0., Spar=0., R;
int counter=0;
for (i=0; i<n; i++ ) {
printf("Dwste thn timh thw antistashs se om\n");
scanf("%f", &R);
if (R>0) {

if ( (Imin<=V/R) && (V/R<=Imax) ) {
Srow=Srow+R;
Spar=Spar+1./R;
counter=counter+1;
if (i==0){
Rmin=R; }
else {
if (R>Rmax){
Rmax=R;} // end if
if (R<Rmin) {
Rmin=R; } // end if
} // end if

}
else {
printf("H entash tou reumatos pou diaperna auth thn antistash den anhkei sta epitrepta oria\n"); } // end if
} // end if
else{
printf("H timh thw antistashs prepei na einai megaluterh tou mhdenos\n"); } // end if
} // end for
Spar=1./Spar;
printf("%d Oi antistaseis pou eginan dektes htan sunolika\t", counter);
printf("%f H timh ths megaluterhs antistashs htan\t", Rmax, "\n" );
printf("%f H timh ths mikroterhs antistashs htan\t", Rmin, "\n" );
printf("%f An oi antistaseis sundeontan se seira tote h sunolikh antistash tha htan\t" ,Srow, "OM\n");
printf("%f An oi antistaseis sundeontan parallhla tote h sunolikh antistash tha htan\t" ,Spar, "OM\n");

}```

2. I have no idea what your program is supposed to do, or why it's not working.

In the mean time, you know you have to fix your indenting if you need "// end if" comments. The closing of a conditional statement should be clearly demarcated with brackets (on their own lines) and the decrementing of the indent level.

3. Originally Posted by jimgeor
the results i take after i compile it are wrong
You need to explain this in more detail. When you do so, distinguish between problems that happen when you compile (errors, warnings) and problems that happen when you run the resulting executable. If you don't understand this difference, tell us what operating system and compiler you are using.

If this is your first C program, it is way too much to have written all at once. What's wrong with "Γεια σας κόσμο"?

Originally Posted by memcpy
you know you have to fix your indenting if you need "// end if" comments.
The indentation is not so good, but "end of whatever" comments are not by definition a bad practice (although these ones are not very good examples, since "end if" all by itself is pretty meaningless -- which if? what task is complete at this point?)

4. It's all Greek to me.

5. Now that I look at it, there's clearly a problem in these printf statements:
Code:
```printf("%d Oi antistaseis pou eginan dektes htan sunolika\t", counter);
printf("%f H timh ths megaluterhs antistashs htan\t", Rmax, "\n" );
printf("%f H timh ths mikroterhs antistashs htan\t", Rmin, "\n" );
printf("%f An oi antistaseis sundeontan se seira tote h sunolikh antistash tha htan\t" ,Srow, "OM\n");
printf("%f An oi antistaseis sundeontan parallhla tote h sunolikh antistash tha htan\t" ,Spar, "OM\n");```
This shows a complete misunderstanding of how printf works.
Presumably you mean
Code:
```printf("Oi antistaseis pou eginan dektes htan sunolika %d\n", counter);
printf("H timh ths megaluterhs antistashs htan %f\n", Rmax);
printf("H timh ths mikroterhs antistashs htan %f\n", Rmin);
printf("An oi antistaseis sundeontan se seira tote h sunolikh antistash tha htan %f OM\n", Srow);
printf("An oi antistaseis sundeontan parallhla tote h sunolikh antistash tha htan %f OM\n", Spar);```

6. this is the the exercise i'm supposed to do:

A series of n ohmic resistors are available for the construction of a circuit. To accept a resistance should the intensity of the current flowing through it, if it applied to a voltage V, is less than a maximum value Imax and greater than a minimum value Imin.
Write a program that, after reading n the number of resistors, the voltage V and Imin and Imax values ​​define a recycling at each iteration of which reads a value of n resistors and check if it is accepted or not. The program prints the number of resistors that were accepted, the value of the largest and smallest of these and the resistance value will occur if the accepted resistors connected in series and if they are connected in parallel
Note: Do not use tables.
When you find a resistor that is not accepted message to be printed.

@MK27 I don't get any errors or warnings from the compiler.The problem is that the results have no logic. Btw i use codeblocks-mingw as a compiler (the one that this site suggests) and i have win7-32bit
@oogabooga I changed what you said in my code but i still get the same illogical results

7. after some tests i realised that the problem is in this part of the programm
Code:
```  if (i==0){
Rmin=R; }
else {
if (R>Rmax){
Rmax=R;} // end if
if (R<Rmin) {
Rmin=R; } // end if
} // end if```
but i can't find it. Someone help me please?

8. Finding the min and max in data is simple:
Code:
```Set min and max BOTH to the first data item. Then loop through all the data:

for(each data item) {
if(data item > max)
max = data item;
if(data item < min)
min = data item;
}```
It's simple, it's accurate. I suggest you use it.

9. Thanks a lot adak it worked perfectly!!

10. this is my final programm and it works perfect

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

int main(){
float V, Imax, Imin;
int n, i;
printf("Dwste ton arithmo twn antistasewn\n");
scanf("%d", &n);
printf("Dwste thn timh ths tashs V\n");
scanf("%f", &V);
printf("Dwste thn megisth timh ths entashs tou reumatos\n");
scanf("%f", &Imax);
printf("Dwste thn elaxisth timh ths entashs tou reumatos\n");
scanf("%f", &Imin);
float Rmax=0., Rmin=V/Imin, Srow=0., Spar=0., R;
int counter=0;

for (i=0; i<n; i++ ) {
printf("Dwste thn timh thw antistashs se om\n");
scanf("%f", &R);

if (R>0) {

if ( (Imin<=V/R) && (V/R<=Imax) ) {
Srow=Srow+R;
Spar=Spar+1./R;
counter=counter+1;

if (R>Rmax){
Rmax=R;} // end if
if (R<Rmin) {
Rmin=R; } // end if

}
else {
printf("H entash tou reumatos pou diaperna auth thn antistash den anhkei sta epitrepta oria\n"); } // end if
} // end if
else{
printf("H timh thw antistashs prepei na einai megaluterh tou mhdenos\n"); } // end if

} // end for

printf("Oi antistaseis pou eginan dektes htan sunolika %d\n", counter);
printf("H timh ths megaluterhs antistashs htan %f\n", Rmax);
printf("H timh ths mikroterhs antistashs htan %f\n", Rmin);
printf("An oi antistaseis sundeontan se seira tote h sunolikh antistash tha htan %f OM\n", Srow);
printf("An oi antistaseis sundeontan parallhla tote h sunolikh antistash tha htan %f OM\n", 1./Spar);
}```

11. Something else to bear in mind when you're coding.
SourceForge.net: Indentation - cpwiki