-1
Example input: 20 10 5 20 2 20 20 20 2 2 0

Output:
(20*5)
(10*1)
(5*1)
(2*3)

I just started programming this semester and need help on a project. I apologize if my question is unclear.

So basically I have to input positive integers till I enter "0" would end the program. I'm not allowed to use arrays(whatever that means).

#include <stdio.h>


int main ()
{
int number, count=0

while(1)
{
    scanf("%d",&number);
    if (number!=0)
    {
        count++; continue;
    }
    else
    {
        printf("%d*%d",number,count);
        break;
    }
    return 0;
}

How do I store these multiple numbers so that I wouldn't overlap the previous number and to increment duplicate numbers by 1 every time it's entered? I can't ask my professor for help; he just tells me to google it.

"A certain engineering apparatus is controlled by the input of successive numbers (integers). If there is a run of the same number, the apparatus can optimize its performance. Hence we would like to arrange the data so as to indicate that a run is coming. Write a C program that reads a sequence of numbers and prints out each run of numbers in the form (n∗m) where m is the number repeated n times. Note that a run can consist of just a single number. The input numbers are terminated by a zero, which halts the apparatus."

Beginner Programmer
  • 167
  • 1
  • 2
  • 11
  • Why are you not allowed to use arrays is questionable? You want to count the frequency, that can be done effectively with arrays. For an idea, you can check [this post](http://stackoverflow.com/questions/11053607/count-how-many-times-an-element-occurs-in-an-array-java). – Mohit Jain Sep 30 '14 at 04:46
  • If he specifically said you can't use arrays, you could use a linked list, but I think that is going against the spirit of the assignment. – IllusiveBrian Sep 30 '14 at 04:46
  • sorry its supposed to be number – Beginner Programmer Sep 30 '14 at 04:46
  • Not sure how arrays work but we haven't gone over arrays in class. So I'm guessing it's for the people who have experience with programming? – Beginner Programmer Sep 30 '14 at 04:48
  • Perhaps you should tell us what you have gone over in class, because this assignment requires a data-structure and arrays are usually where you start. – Elliott Frisch Sep 30 '14 at 04:49
  • We've gone over basic arithmetic operators, flow control such as: if else, switch cases, while loop, for loop, and goto statements(not supposed to use goto). – Beginner Programmer Sep 30 '14 at 04:53
  • @Namfuak what do you mean by linked list? – Beginner Programmer Sep 30 '14 at 04:56
  • 3
    @BeginnerProgrammer You can look it up if you want, Wikipedia seems to have a decent explanation, but if your professor hasn't taught them to you yet I doubt his intention is for you to use them. Could you post the full text of the assignment? As you've stated it, it does not seem reasonable without using arrays. – IllusiveBrian Sep 30 '14 at 05:00
  • The natural way to solve this is to have an array/hash , this seems like a very contrived exercise. – AndersK Sep 30 '14 at 05:01
  • Your sample output does not match your sample input given your directions. – Elliott Frisch Sep 30 '14 at 05:09
  • @Namfuak I posted the text of the assignment just now. He doesn't list it in the assignment but has specifically told the whole class not to use arrays. He did mention about using a text file for these numbers and run the program with the text file, containing the numbers shown as input. – Beginner Programmer Sep 30 '14 at 05:10
  • @ElliottFrisch Yes it doesn't, but that is what I have so far and I am confused on what to do now. – Beginner Programmer Sep 30 '14 at 05:11
  • @BeginnerProgrammer Is that sample output from the assignment, or is it what you think the assignment is asking? – IllusiveBrian Sep 30 '14 at 05:12
  • @Namfuak It's the sample output. – Beginner Programmer Sep 30 '14 at 05:15
  • 1
    The assignment says `(n*m)`, but the output appears to be in the form `(m*n)`. That said, based on your teacher's response to questions I would say the way to complete this assignment is to find a new teacher. – IllusiveBrian Sep 30 '14 at 05:17
  • @Namfuak He tends to make small mistakes where students have to correct him. – Beginner Programmer Sep 30 '14 at 05:24
  • @BeginnerProgrammer: in that case you tell him : – chouaib Sep 30 '14 at 05:26
  • @BeginnerProgrammer You sample output is not called a "run", it is called a "count". Please see my answer. – Masked Man Sep 30 '14 at 05:32
  • 2
    @chouaib What's sort of funny is that he is using arrays for formatting `scanf` and `printf`, so that breaks that rule as well. – IllusiveBrian Sep 30 '14 at 05:35
  • 2
    @Namfuak A professor who doesn't know the difference between "run" and "count" probably wouldn't notice the const char array. :P – Masked Man Sep 30 '14 at 05:37
  • Your example output looks wrong; did you make that up or did it come from the teacher? – David Grayson Sep 30 '14 at 05:37
  • 1
    @Happy Perhaps the next assignment will be to give a program which returns the count of consecutive page accesses in a memory paging scheme. – IllusiveBrian Sep 30 '14 at 05:39

1 Answers1

3

This assignment seems to be based on half-baked knowledge of run length encoding (RLE). Anyway, here's a pseudo-code which does what it asks.

in = read next number from input
current_num = in    // let the 1st number in list be current_num
count = 1

loop 
    in = read next number from input
    if (in == 0)  break   // we are done, get out of loop

    else if (in == current_num) count += 1

    else     // run has ended, print it and start new run
        print current_num * count
        current = in
        count = 1

end loop

print current_num * count    // we exited the loop before printing the last run
                              // so do it outside the loop

You can implement it in code and then "optimize" it to remove repeated code, and take care of corner cases (such as "empty" input, single number input, etc.)

Edit Just to be clear, the assignment asks for a 'run' of numbers, but the sample output shows a 'count' of numbers. These two are not the same.

Masked Man
  • 1
  • 7
  • 40
  • 80
  • this doesn't much the output above ! – chouaib Sep 30 '14 at 05:32
  • While this is what I thought the code was probably asking for, it doesn't fit the assignment since the sample output appears to have read and organized the whole run before printing it, rather than only printing consecutive duplicates. This makes more sense than the actual assignment though. – IllusiveBrian Sep 30 '14 at 05:32
  • @chouaib I have commented the same on the question. The assignment asks for a 'run' of numbers, but the so-called sample output shows a 'count' of numbers. The sample output given in the question is wrong. – Masked Man Sep 30 '14 at 05:35
  • opps ! sharp vision here is my +1 – chouaib Sep 30 '14 at 05:40