0

Here in this program we take input from the user and then we scan the given input .If a number is present odd number of times then we find the product of that particular number with number of times its occuring. In this program if i give an input as 1 1 2 2 3 3 3 then in this case the output must be 9 but i'm getting it 999.

#include <stdio.h>

int main()
{
int data[10],i,j,sum,count=0,num;
for(i=0;i<7;i++)
{
    scanf("%d",&data[i]);
}
for(i=0;i<7;i++)
{
    num=data[i];
    for(j=0;j<7;j++)
    {
        if(num==data[j])
        {
            count++;
        }
    }
    if((count%2)!=0)
    {
        printf("%d",num*count );
    }
    count=0;
}
}
  • 2
    This seems like a good time to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Jan 31 '19 at 07:02
  • It would be easier to understand if you set `count = 0;` just before you start the loop that increments it, rather than initializing to zero a long time before that loop starts, and then resetting `count = 0;` just before cycling back. You could also declare `count` (and `num`, and `j`) at smaller scopes than you do. – Jonathan Leffler Jan 31 '19 at 07:02
  • 2
    The basic problem is that each time you come across `3`, you count how many times it appears — and it appears 3 times, so you print `9` on each occasion. You're going to need to keep a track of which numbers you've previously processed, so you don't keep repeating yourself. – Jonathan Leffler Jan 31 '19 at 07:04
  • Hello @JonathanLeffler ! You needn't keep track of it.Check out m solution. – ahaan abrar Jan 31 '19 at 07:15

1 Answers1

0
#include <stdio.h>

int main()
{
int data[10],i,j,sum,count=0,num;
for(i=0;i<7;i++)
{
    scanf("%d",&data[i]);
}
for(i=0;i<7;i++)
{
    num=data[i];
    for(j=0;j<7;j++)
    {
        if(num==data[j])
        {
            count++;
        }
    }
    if((count%2)!=0)
    {
        printf("%d",num*count );
    }
    i=i+(count-1);
    count=0;

}
}
  • I tried solving and i found an answer! If you guys have a better solution please do share. – ahaan abrar Jan 31 '19 at 07:12
  • Okay @JonathanLeffler that makes sense. What do i do now? – ahaan abrar Jan 31 '19 at 07:37
  • Somehow or other, you keep track of which numbers you've processed. If it is OK to modify the array and all the data values are positive, you could set a value to `-1`, for example, to indicate that it has 'been used'. Your scanning code would then skip the `-1` values. Or you could have a secondary array in which you keep a record of which numbers you've processed (and you'd probably have a counter to tell you how many you've processed), and then when you start on a new number, check whether it's been processed before. This leaves the original data alone. There are other possibilities. – Jonathan Leffler Jan 31 '19 at 07:40
  • Yeah this might work! I'll check it out. @JonathanLeffler – ahaan abrar Jan 31 '19 at 07:47
  • I'd like to ask you something out of the blue! How do i break a loop using enter key? @JonathanLeffler – ahaan abrar Jan 31 '19 at 07:51
  • Hmmm; that's going to take some explaining — two lots of explaining. (1) You need to explain what you're after more clearly (and you should probably specify whether you're on Windows or a Unix-like system, or something else), and (2) I'll need to dig out questions about 'canonical vs non-canonical input' and related topics. Normally, a terminal won't deliver any data to your program until the user types enter; so you could read to the end of line (that's not a job for the `scanf()` family of functions — use `getchar()` and friends), and stop when you detect the newline. – Jonathan Leffler Jan 31 '19 at 07:58
  • Let us imagine that I'm taking an array of integers as an input and once I press enter it must break the loop and give me an output. I don't think getchar would be of any help in this case. @JonathanLeffler – ahaan abrar Jan 31 '19 at 08:15
  • You are looking to read a line of numbers and then process that line of numbers (where different lines can have different size lists of numbers on them). You're right that `scanf()` is no longer the tool for the job; it is fundamentally heedless of newlines (they're just white space, and it skips white space before converting numbers). _[…continued…]_ – Jonathan Leffler Jan 31 '19 at 17:08
  • _[…continuation…]_ The correct technique is to use [`fgets()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html) or POSIX [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) to read lines of data, and then use [`sscanf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/sscanf.html) to read the numbers. You need to [use `sscanf()` in a loop](https://stackoverflow.com/questions/3975236/how-to-use-sscanf-in-loops). – Jonathan Leffler Jan 31 '19 at 17:09
  • This might take some time! BTW thanks! @JonathanLeffler – ahaan abrar Feb 01 '19 at 17:54