Thread: I'm a beginner. HELP!

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    17

    Question 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. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    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. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jimgeor View Post
    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 "Γεια σας κόσμο"?

    Quote 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?)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It's all Greek to me.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    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. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    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. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    Thanks a lot adak it worked perfectly!!

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    this is my final programm and it works perfect

    thanks for your help!!!

    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. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Something else to bear in mind when you're coding.
    SourceForge.net: Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner help
    By mouse666666 in forum C++ Programming
    Replies: 5
    Last Post: 09-27-2010, 05:07 AM
  2. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM
  3. beginner ?
    By braincrash in forum C Programming
    Replies: 2
    Last Post: 02-18-2003, 03:33 AM
  4. Help (Beginner)
    By NewbieC in forum C++ Programming
    Replies: 9
    Last Post: 01-29-2003, 02:26 PM
  5. C++ beginner
    By fishcrazzy2000 in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2002, 10:49 PM