sum all odds number in array.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • khaichiew85
    New Member
    • Feb 2009
    • 10

    sum all odds number in array.

    i am trying to write a C code which request input from user and store them in array.Number of inputs is determined by the user and it is limited to 100 inputs.Then I need to pass the array to recursive function to sum all odd values in the array.Finally,r eturn the sum of all odd numbers to the user.

    However, i got the error which stated that the system cannot find the file specified.
    So I am not sure if my code is correct.
    I am using Visual C++ express 2005.

    Below is my code:

    #include<stdio. h>

    int sumOdd(int x);
    int main ()
    {
    int arr[100];
    int readNum;
    int sumAllOdd;
    int i;

    printf("You may enter up to 100 numbers.\nPleas e enter an integer:");
    scanf_s("%d",&r eadNum);

    if(readNum>100)
    printf("Error.I t exceeds 100 numbers.\nPleas e enter an integer:");

    for( i = 0; i < readNum; i++)
    scanf_s("%d",&a rr[i]);

    sumAllOdd = sumOdd();
    }

    int sumOdd(int x)
    {
    int i;
    int temp=0;
    int arr[100];
    for(i=0;i<100; i++)
    {
    if(arr[i]%2==0)
    return 0;
    else
    temp += arr[i];
    return 1;
    }
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Can not find which specified file? Was this during compilation, linking or running?

    Comment

    • khaichiew85
      New Member
      • Feb 2009
      • 10

      #3
      re:When i try to execute the program

      when i try to execute the program i got the error "The system cannot find the file specified"

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Then you have probably got the name or the location of the executable file wrong.

        Comment

        • khaichiew85
          New Member
          • Feb 2009
          • 10

          #5
          i just click save and build project.then only i execute.which part i went wrong?any suggestion?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            How are you executing the program? From Visual Studio or from the Command Interpreter?

            If it's the Command Interpreter, you have to enter the complete path to the .exe file.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Did you check the build result to make sure it actually produced an executable file?

              Comment

              • khaichiew85
                New Member
                • Feb 2009
                • 10

                #8
                i can execute it already.but there are some errors there.Anyone can help?
                how to write it in recursion form?

                #include<stdio. h>

                int sumOdd(int arr[]);
                int main ()
                {
                int arr[100];
                int readNum;
                int sumAllOdd;
                int temp;
                int i;

                printf("You may enter up to 100 numbers.\nPleas e enter an integer:");
                scanf("%d",&rea dNum);

                if(readNum>100)
                {
                printf("Error.I t exceeds 100 numbers.\nPleas e enter an integer:");
                scanf("%d",&rea dNum);

                for( i = 0; i < readNum; i++)
                {
                temp += arr[i]
                }
                }sumAllOdd = sumOdd(2);
                }

                int sumOdd(int x)
                {
                int i;
                int temp=0;
                int arr[100];
                for(i=0;i<100; i++)
                {
                if(arr[i]%2==0)
                return 0;
                else
                temp += arr[i];
                return 1;
                }
                }

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  First fix your iterative version because it's adding garbage values; you defined an array arr local to that function so it's using that array. Better pass the other array as a parameter to that function (and pass its length also).

                  kind regards,

                  Jos

                  Comment

                  • khaichiew85
                    New Member
                    • Feb 2009
                    • 10

                    #10
                    problem stil exist..

                    im quite new to write a program..After few days break,i try to do the coding once again.But problem is still there.Why my loop keep on repeating instead of jumping to the next statement?Does my recursive function there correct?

                    #include<stdio. h>

                    int sumAllOdd(int [],int );
                    int main (void)
                    {
                    int i = 0;
                    int input;
                    int arr[100];
                    char ans;

                    printf("\nPleas e enter amount of integer for input.(Maximum is 100 numbers only\n) : ");
                    scanf ("%d",&input );

                    do{
                    printf("Please enter amount of integer for input.(Maximum is 100 numbers only) : ");
                    scanf ("%d",&input );

                    arr[i++] = input;
                    }while(input>0 && i <=100);


                    if(input==100)
                    {
                    printf("\nMaxim um amount of number reached\nThank you.");
                    printf("\nTotal sum of all odd numbers is %d",sumAllOdd(a rr,0));
                    }
                    do{
                    printf("\nDo you still want to continue?( Y to continue or N to quit\n");
                    scanf ("%c",&ans);

                    if(ans=='y' || ans||'Y')
                    {
                    return main();
                    }
                    else if(ans=='n' || ans=='N')
                    return 0;
                    else
                    printf("Invalid input");
                    return 0;
                    }while(ans=='y' || ans=='Y' || ans=='n' || ans=='N');

                    }
                    int sumAllOdd(int arr[],int x)
                    {
                    if(arr[x]==0)
                    return 0 ;
                    else if(arr[x]%2)
                    return arr[x] + sumAllOdd(arr,x +1);
                    else
                    return sumAllOdd(arr,x +1);
                    }

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      Code:
                      do{
                      printf("Please enter amount of integer for input.(Maximum is 100 numbers only) : ");
                      scanf ("%d",&input);
                      
                      arr[i++] = input;
                      }while(input>0 && i <=100);
                      Your arr array has 100 elements numbered from 0 to 99. This loop will put a value in arr[100] which is outside your array.

                      Then in sumAllOdd, how does arr[x] get to be zero when the data entry loop excludes values that are zero or less?

                      Comment

                      • khaichiew85
                        New Member
                        • Feb 2009
                        • 10

                        #12
                        some refinement.

                        solution is found but what if i want to change the do while loop into for loop?any suggestion?
                        my refinement codes is as below:

                        #include<stdio. h>
                        #include<ctype. h>
                        #include<stdlib .h>

                        int sumAllOdd(int [],int i);

                        int main (void)
                        {
                        double input;
                        int count;
                        int arr[100];


                        printf("Please enter number of inputs:(Max is 100):\n");
                        scanf ("%lf",&inpu t);

                        fflush(stdin);

                        /* do{
                        printf("<Press 0 to exit>\nInput:") ;
                        scanf("%lf",&in put);
                        fflush(stdin);

                        if( (input/1) == ( (int)input) )
                        {
                        arr[count++] = input;
                        }
                        else
                        {
                        printf("Integer number only.");
                        count--;
                        }
                        }while(input && count<=100);

                        */

                        if(count==100)
                        printf("Maximum number of input reach.");
                        printf("\nSum of all odd numbers is %d\n\n",sumAllO dd(arr,0));
                        }
                        int sumAllOdd(int arr[],int i)
                        {
                        if(arr[i] == 0 || i == 100)
                        return 0;
                        else
                        {
                        if(arr[i] % 2 == 1)
                        return arr[i] + sumAllOdd(arr,i +1);
                        else
                        return sumAllOdd(arr,i +1);
                        }



                        }


                        Originally posted by weaknessforcats
                        Code:
                        do{
                        printf("Please enter amount of integer for input.(Maximum is 100 numbers only) : ");
                        scanf ("%d",&input);
                        
                        arr[i++] = input;
                        }while(input>0 && i <=100);
                        Your arr array has 100 elements numbered from 0 to 99. This loop will put a value in arr[100] which is outside your array.

                        Then in sumAllOdd, how does arr[x] get to be zero when the data entry loop excludes values that are zero or less?

                        Comment

                        • weaknessforcats
                          Recognized Expert Expert
                          • Mar 2007
                          • 9214

                          #13
                          Maybe:

                          Code:
                          input = 1;  //entry values to get you into the loop
                          count = 0;
                          while(input && count<=100)
                          {
                              scanf("%d", &input);
                              //etc...
                          }
                          BTW: Why is input a double but the array you store it in is an array of int?

                          Comment

                          • khaichiew85
                            New Member
                            • Feb 2009
                            • 10

                            #14
                            re:

                            im casting the input from the user to int.as a preventive way if user key in decimal numbers.
                            only if it is an integer then it will store it as an array.


                            Originally posted by weaknessforcats
                            Maybe:

                            Code:
                            input = 1;  //entry values to get you into the loop
                            count = 0;
                            while(input && count<=100)
                            {
                                scanf("%d", &input);
                                //etc...
                            }
                            BTW: Why is input a double but the array you store it in is an array of int?

                            Comment

                            • weaknessforcats
                              Recognized Expert Expert
                              • Mar 2007
                              • 9214

                              #15
                              That's not going to work. The user could type in ABC which isn't a double and scanf will fail.

                              You can't use scanf if you don't know the format of the input. You need to use a %c or %d or %f or something and that means kyou know ahead of time what is being entered.

                              If you have no idea what's being ented than you have to use gets and receive th input a s string and then parse the characters inthe string to figure out what was entered.

                              I recommend for beginners that you stick with giving the program what it expects.

                              BTW: Your typecast of double to int just suppresses a compiler warning about loss of data as the decimals are chopped off the data. There is no way to convert a double to an int as the int has no decimal places.

                              Comment

                              Working...