Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Help for homework


    Hi guys, hope you are well

    I have homework

    I tried to solve it but I couldn't

    anyone can plz?


    Write a program that sums a sequence of integers. Assume that
    the first integer read with scanf specifies the number of values remaining to be entered. Your program
    should read only one value each time scanf is executed. A typical input sequence might be
    5 100 200 300 400 500
    where the 5 indicates that the subsequent five values are to be summed.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    So show us what you tried.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    its easy program but u should at least try something.
    and if u tried anything then show it.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0
    int arr[a];
    int s,d;


    printf("Enter the number");
    scanf("%d", a);

    for(s=0; s<a; s++)

    {


    printf("Enter number%d:", s);
    scanf("%d", arr[a]);


    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by Rsh_rsh
    int arr[a];
    int s,d;


    printf("Enter the number");
    scanf("%d", a);

    for(s=0; s<a; s++)

    {


    printf("Enter number%d:", s);
    scanf("%d", arr[a]);


    }
    i'm beginner in this language
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    We were all beginners at one time.

    Start by writing down in English how you would do that by hand. Then expand that, again in English, with a step-by-step procedure. Then once you know what you're trying to do, you translate each English step into C.
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally Posted by Rsh_rsh
    int arr[a];
    int s,d;


    printf("Enter the number");
    scanf("%d", a);

    for(s=0; s<a; s++)

    {


    printf("Enter number%d:", s);
    scanf("%d", arr[a]);


    }
    Use code tags for posting code. Otherwise the code's indentation, which is absolutely needed to keep it readable, will be lost.

    [code] insert your formatted code listing here [/code]
    Eg:
    Code:
    int arr[a];
      int s,d;
      
             printf("Enter the number");
             scanf("%d", a);
             
             for(s=0; s<a; s++)
             {
                  printf("Enter number%d:", s);
                  scanf("%d", arr[a]);
       
             }
    The variable a needs to be declared.

    You cannot use a in the declaration for arr. There is a common way to create an array after you are told how big to make it, but you are obviously not at that point yet. Decide on some maximum size for arr and use that to declare it. Of course, if you are not expected to display those array value later, then you would have no need for an array.

    scanf needs variable addresses, not values! Use the address operator (&) as your book has told you to do.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by dwise1_aol
    We were all beginners at one time.

    Start by writing down in English how you would do that by hand. Then expand that, again in English, with a step-by-step procedure. Then once you know what you're trying to do, you translate each English step into C.
    thank you, i'll do it now ;)
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by dwise1_aol
    Use code tags for posting code. Otherwise the code's indentation, which is absolutely needed to keep it readable, will be lost.

    [code] insert your formatted code listing here [/code]
    Eg:
    Code:
    int arr[a];
      int s,d;
      
             printf("Enter the number");
             scanf("%d", a);
             
             for(s=0; s<a; s++)
             {
                  printf("Enter number%d:", s);
                  scanf("%d", arr[a]);
       
             }
    The variable a needs to be declared.

    You cannot use a in the declaration for arr. There is a common way to create an array after you are told how big to make it, but you are obviously not at that point yet. Decide on some maximum size for arr and use that to declare it. Of course, if you are not expected to display those array value later, then you would have no need for an array.

    scanf needs variable addresses, not values! Use the address operator (&) as your book has told you to do.
    first of all thanks for valuable information

    and I thought I need array

    anyway

    im gonna read a lot, I'll try to make it

    I started to learn c one week a ago

    i'll try to next time to say I made it hopeful :D
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by dwise1_aol
    We were all beginners at one time.

    Start by writing down in English how you would do that by hand. Then expand that, again in English, with a step-by-step procedure. Then once you know what you're trying to do, you translate each English step into C.



    I need from the program to ask me "how many integers do you want"

    Code:
    int a;
    
           printf("how many integers do you want\n");
          
           scanf("%d", &a);




    then I can choose any number
    lets say 3
    then after that
    the program ask me "enter first number"
    then I entered 10
    then ask me again
    "enter second number"
    I entered 100
    then ask me again
    "enter third number"
    I entered 1000


    Code:
    for(b=0; b<; s++)
    
    {
    
           printf("Enter number %d\n", b);
          
           scanf("%d", &b);
    
    }





    then the program add them together

    and show the result

    it'll be 1110


    what do you think

    this is what I can do
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0
    Hi guys

    after many hours

    I got this

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      
     
      int arr[24],b,s;
         
        
        
        printf("How many integers do you want\n\n");
             scanf("%d", &s);
             
         
         for(b=1; b<=s; b++)
         
         {
         
             printf("Enter Number%d:", b);
             scanf("%d", &arr[b]);
      
        }
        
       printf("******************- LIST-****************\n");
        
         
         for(b=1; b<=s; b++)
         
         {
         
            // printf("arr%d: %d\n", b, arr[b]);
             
             printf("Number%d\n+\n",b );
      
        }
     
     
    
      system("PAUSE");	
      return 0;
    }
    but I wanna add them to get the result

    can you help me for that

    cause I tried but I couldn't

    thanks in advance
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0
    Hello,

    First, you need to declare & define four integers: number of integers you will be entering, a counter for the for loop, num to store the entered number and the sum.

    Here is the actual code:

    <edited - please don't help with complete code. Hints are ok>
    Last edited by Scorpions4ever; March 28th, 2013 at 01:27 AM.
  24. #13
  25. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    Originally Posted by Rsh_rsh
    Hi guys

    after many hours

    I got this

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      
     
      int arr[24],b,s;
         
        
        
        printf("How many integers do you want\n\n");
             scanf("%d", &s);
             
         
         for(b=1; b<=s; b++)
         
         {
         
             printf("Enter Number%d:", b);
             scanf("%d", &arr[b]);
      
        }
        
       printf("******************- LIST-****************\n");
        
         
         for(b=1; b<=s; b++)
         
         {
         
            // printf("arr%d: %d\n", b, arr[b]);
             
             printf("Number%d\n+\n",b );
      
        }
     
     
    
      system("PAUSE");	
      return 0;
    }
    but I wanna add them to get the result

    can you help me for that

    cause I tried but I couldn't

    thanks in advance
    take a sum variable and in 1st loop after scanning add scanned data in the sum variable.
  26. #14
  27. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally Posted by Rsh_rsh
    but I wanna add them to get the result

    can you help me for that

    cause I tried but I couldn't
    Adding is one of the first things we learned in arithmetic. In C it's
    c = a + b;
    You can't get much more basic than that.

    Since you're storing the input values in an array, use a for-loop to iterate through the array, adding each element to a sum variable that you declare. When you are done, that sum variable will contain the sum, which is what you are trying to calculate.

    Remember this very basic algebraic identity:
    a + 0 = a
    which means that any value added to zero is itself. So when you declare that sum variable, initialize it to zero. Then when you've added all the elements of the array to sum, it will contain the sum of the elements of the array. Very simple.

    Even simpler would be to simply add each number to sum when you input it. That would eliminate any need for an array. Unless you absolutely need to keep each input value for later (eg, to display them in a final report, to use them in another calculation).


    Please review your textbook's chapter on arrays, paying especial attention to indexing. In C, an array's first element is indexed by zero, not by one. When you declare an array with N elements, you index them with zero through N-1. In your case, your array arr, having a size of 24, consists of the elements arr[0] through arr[23].

    You are starting your array operations with 1, not with 0 as you should be doing, and thus ignore the first element. You can get away with that in this program, but only if you input fewer than 24 values. Your for-loops should read like this:
    for(b=0; b<s; b++)
    which will iterate s times.
    Last edited by dwise1_aol; March 28th, 2013 at 10:49 AM.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0
    I made it

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      
     
      int arr[0];
      int a=0,b,s,sum=0;
         
        
        
        printf("How many integers do you want\n\n");
             scanf("%d", &s);
             
         
         for(b=1; b<=s; b++)
         
         {
         
             printf("Enter Number%d:", b);
             scanf("%d", &arr[b]);
             sum = arr[b] + sum;
      
         }
        
        
       printf("******************- LIST-****************\n\n");
        
         
          for(b=1; b<=s; b++)
         
         {
          
             printf("Number%d\n+\n",b );
        }
     
      printf("The total is %d\n", sum);
    
      system("PAUSE");	
      return 0;
    }


    I would like to thank

    1- dwise1_aol :cheers:

    2- eramit2010 :hi:

    3- winniethedev :thumbs:

    I know that it was easy program

    but really one day i'll be really good in c

    be cause you guys

    thank you very much

    an have nice and amazing weekend ;)
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo