help with for loop with arrays

This is a discussion on help with for loop with arrays within the C Programming forums, part of the General Programming Boards category; can someone tell me what am i doing wrong with for loop. its not returning what its suppose to. everything ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    19

    help with for loop with arrays

    can someone tell me what am i doing wrong with for loop. its not returning what its suppose to. everything else works except for that.


    Code:
    #include <stdio.h>
    
    int min();
    int max();
    
    main()
    {
          double x[10], y[10], z[19];
          int c;
          int f;
    
            FILE *inp;
            int input_status;
            int i,t;
            double a,b;
    
            inp = fopen("input-xy.txt", "r"); //opens files
            input_status = fscanf(inp, "%d %lf %lf", &i,&a,&b); //takes digits
    
            while(input_status != EOF)
            {
    
                    x[i]=a;
                    y[i]=b;
    
                    input_status = fscanf(inp, "%d %lf %lf", &i,&a,&b);
            }
                    fclose(inp);
            for(t=1;t<=6;t++)
            printf("%lf %lf\n",x[t],y[t]);
    
    
          int j,q;
          double sum;
    
          for(j=0; j<=18; ++j)
          {
                c = max(j);        //cant figure out whats wrong with it
                f= min(j);
                sum=1;
                
                for(c;c<=f;c++)
                    {  q=x[c]*y[j-c];
                       sum= sum + q;
                    }
                  z[j]=sum;
                  printf("z[%d]= %lf\n",j,z[j]);
          }
    system("pause");
    
    }
    int min(int j)
    {       int min;
            if(9>=j)
               min=j;
            else if(9<=j)
            min=9;
            return min;
    }
    
    int max(int j)
    {       int max;
            if((9-j)<=j)
               max=j;
            else if((9-j)>=j)
            max=(9-j);
            return max;
            }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why do you think there's something wrong with it?

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    input_status = fscanf(inp, "&#37;d %lf %lf", &i,&a,&b);

    Is that what fscanf() returns?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    19
    yeah. the scanf works right i checked it. tje while loop assigns the elements arrays. but the for loop that is suppose to give me the summation doesnt work. since i say sum=1 it gives me 1 when i print


    printf("z[%d]= %lf\n",j,z[j]);


    . so it has to be that loop that isnt working properly. i am not sure whats wrong with it.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So when j equals zero, your max returns 9 and your min returns 0. Are you sure your for loop isn't supposed to go from min to max, rather than the other way around?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21