You are here:

C/programming language

Advertisement


Question
QUESTION: 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 4 2 9 9 3 7 7 7 3 3 -1

-give me any clue what does it mean. i cannot do it

ANSWER: Hello Ashiela.
I think the question is asking you to read in a series of numbers from the keyboard until the -1 is read.  Each number should be added to a total. That will find the sum of the numbers. I think the second part of the question is asking to count the number of times each number appears. For example, in your question there is one 4, three 7s, and two 9s. Of course you should ask your instructor if that is what the problem means.

You should work on the reading from the keyboard, and the calculation of the total first. When you have that done, then add code to count the 'incidences'.

You will need an integer to keep track of the total. Don't forget to start the total at 0.
You will need a loop
You can use scanf to read the numbers, unless your instructor suggests something else.

Show me an attempt at the problem and I will help you more.

Best regards
Zlatko

---------- FOLLOW-UP ----------

QUESTION: i've done some methods buut still couldnt get it, can you check for me
main()
{
     int x=2, y=0,sum=1 n;
     scanf("%d",&n);
     while(x=2)
  {

     
     scanf("%d
", sum);
     x=x+y;
  }
}

Answer
Hello.

Well that is a start, but you have to start your sum at 0, not at 1. There is no reason to ask the user for the sum, as you are doing with scanf. The sum is what the program is to calculate. I think the fact that the input is at least 2 integers is not important. Maybe it is there to confuse you.

Here is a working program. Try to read it and understand the logic.


#include <stdio.h>

int main(void)
{
   
   int sum=0; /* start your sum at 0 */
   int input;
   do
   {
       scanf("%d", &input); /* read in the input */
       if (input >= 0)
       {
         sum = sum + input; /* add to the sum only if the input is greater than 0 */
       }
   } while(input >= 0); /* stop the loop when x is less than 0 */

   printf("The sum is %d\n", sum);

   return 0;
}

Now it remains to count the number of each digit. Did you find out from your instructor if that is what is to be done?

I would assume that each number is between 0 and 9. You should check that with your instructor. If you have an array of 10 integers for the counters, then the users input can be used as an index into the array. For example, declare the array like this:

int counts[10] = {0,0,0,0,0,0,0,0,0,0};


Then you can use input as an index into the array like this:

counts[input] = counts[input] + 1;

So when the user inputs 7, the '7' slot of the counter gets incremented.

Try to put those two ideas together into the program I gave you. Then add code to print out the count. Remember the array idea works only if the input is a single digit.

Best regards
Zlatko

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.