Thread: C programming Language

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    C programming Language

    Write a program in C program to find the sum of the following:
    The input contain a sequence of two or more positive integers terminated by -1. Write a piece of code to count the 'incidence' in this sequence (i.e. the number of pairs of equal, adjacent numbers). For example, the following sequence contains 4 incidences:
    4 2 9 9 3 7 7 7 3 3 -1




    what method should i used? any example can show me so that i can more understand

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    See also here, where this was previously posted.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Read each number in the list. Remember it in some variable. When you read the next number, if it equals the one before it, increase a counter called incidence (which before the loop is initialized to 0).

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    claudiu:

    ok but for this question do i should use array ?

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    From what I gather you don't need to use an array, because you only need to remember the count of incidence not the numbers that repeat themselves.

    So you should:

    1) read current number in variable 'x'
    2) store 'x' in y
    3) read next number in 'x'
    4) if new 'x' == y then this is a repetition so increase the count
    5) store new'x' in y to compare it with the next input value

    .... etc.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    int data(3)

    int main()
    {
    cout << sizeof (int) << "\n"; //size of an integer
    cout <<sizeof (short int) << "\n\n"; //size of
    //short integer
    cout <<sizeof (data(01) << "\n"; //size of first
    //int item of data
    cout <<sizeof (data(1) << "\n"; //size of second
    //int item of data
    cout <<sizeof (data(2) << "\n"; //size of third
    //int item of data
    cout <<sizeof << "\n"; //size of int array
    //data

    return 0;
    }


    are my method correct?

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    First of all that is C++ and this is a C forum.

    Secondly, no why are you printing the size of some variables. They are always going to be the same because the array data holds the same kind of data.

    Thirdly, the 'method' you are referring to is actually called an algorithm.

    Lastly, you are not reading any input.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    so my method isnt correct.. can you give me the output so that i can figure it out the program by myself cause im getting confuse

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    can you show me an example or something

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    so if is not.. then i'll try using while?

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    So it's a "give me the code" post in the end. What a surprise.

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ashiela, I gave you the algorithm you need to implement. I am not going to write the code for you though. I am happy to answer questions you may have with your own code but you haven't provided anything like that yet, only some C++ stuff that has nothing to do with what you are trying to do.

    If you are not familiar with C at all perhaps you should read some theory before trying to write this program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-21-2010, 04:40 PM
  2. Replies: 15
    Last Post: 08-09-2009, 11:20 AM
  3. What language did they make Java in?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 07-03-2005, 04:18 PM
  4. Strange loop
    By D@rk_force in forum C++ Programming
    Replies: 22
    Last Post: 12-18-2004, 02:40 PM
  5. Language of choice after C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 06-15-2004, 01:20 AM