Thread: Simple Loop Program

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    18

    Simple Loop Program

    3. Write a program that reads a sequence of positive integers and prints out their sum, except that if the
    same number occurs several times consecutively, ignore all but the first. Assume an input of 0 marks
    the end of the input. For example, if the input is 3 8 5 5 4 9 1 1 1 1 8 0 then you should print 38 (i.e.,
    ignore one of the 5's and three of the 1's).

    Could anyone help me out with this?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,880
    Help you, yes. Do it no.

    Please read this link!
    Announcements - General Programming Boards

    Tim S.

    PS: The Universe is gaining a large lead today.
    Last edited by stahta01; 02-27-2014 at 07:39 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    18
    Sorry about that.
    here is what I have so far
    Code:
    #include <stdio.h>
     
    int main()
    {
    int n, sum = 0, c, value;
    scanf("%d", &);
    
    
    for (c = 1; c != 0; c++)
       {
          scanf("%d",&value);
          sum = sum + value;
       }
     
       printf("%d\n",sum);
    }

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,784
    What is 'c' doing? When will c be equal to 0?

    Also this won't even compile (line 6)

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    18
    Code:
    #include <stdio.h>
    
    int main()
    {
    int n, sum = 0, c, value;
    scanf("%d", &c);
    
    
    for (c = 1; c != 0; c++)
       {
          scanf("%d",&value);
          sum = sum + value;
       }
    
       printf("%d\n",sum);
    }
    


    The c variable should be ending the program as soon as 0 is inputted.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,784
    Quote Originally Posted by sgk1498 View Post
    The c variable should be ending the program as soon as 0 is inputted.
    Explain, in English, what the following line does.

    Code:
    for (c = 1; c != 0; c++)
    


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-13-2013, 08:13 PM
  2. Simple program, stopping invalid character infinite loop
    By samwillc in forum C++ Programming
    Replies: 16
    Last Post: 01-06-2013, 03:18 PM
  3. Simple loop
    By Aash in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2010, 11:15 PM
  4. simple while loop
    By free2rhyme2k in forum C++ Programming
    Replies: 38
    Last Post: 02-01-2008, 08:15 PM
  5. Very simple loop program for payroll
    By alexpos in forum C Programming
    Replies: 17
    Last Post: 10-19-2005, 06:19 PM

Tags for this Thread