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 ‘incidences’ 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

    i've write and please check for me

    main()
    {
    int a,p,j;
    scanf("%d %d %d",&a,&p,&j);

    if (a==p)
    printf("The number of pairs of equal is equal to 1 \n");

    if (p==j)
    printf("The number pairs of equal is equal to 1 \n");
    else

    if (a==p&&p==j)
    printf("contain 2 incidences \n");
    }

    Output :
    1
    1
    9
    the number of pairs of equal to 1
    Press any key to continue

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    That's the general idea. However you are only reading 3 numbers. You need to read a unknown number of numbers and only stop when you encounter -1. So you need a loop for that like so:

    Code:
    int nr_to_be_read = 0;
    
    do{
    
     /* read number */
    scanf("%d", &nr_to_be_read);
    
    }while(nr_to_be_read != -1); /*until the number read will be -1 */

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also please don't open multiple threads on the same problem. Admins will probably merge these.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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