0

I'm beggining to study C programming,so I don't know how to do basic things (may be what I do has no-sense). What I have to do is to get from the user an undefined number of integers from 1-9 and load it to an array, when the user entered 0 or 10 the loop finishes and the array was completed, so I have: int qualifications[SIZEQ] and a function to load it, it works. Now I have to calculate the frequency of each number introduced on this array. I suppose that there are a lot of ways to do it simple (I don't know any), I'm trying to do it with another array, bidimensional, with 2 rows and 10 cols, using 1 row to contain the values from 0 to 9 to compare each number to the numbers of array qualifications[]. And the other row to store the number of times that each number is present on qualifications. I don't know how to do it, I try:

// 1st row of the freq array contains the 1-9 possible qualifications
// 2nd row to count the times that every value appears in qualifications array
// example: 1st row {0,1,2,3...} if 1 repeated 3 times:
//          2nd row {0,2,0,0...}  

int freq[2][10]={{0,1,2,3,4,5,6,7,8,9},{0,0,0,0,0,0,0,0,0,0}};

    int  j=0;   //var to load 0-9 positions of freq[1][j]

    for (int i = 0; i < SIZEQ; i ++)
    {
        if(qualifications[i]==freq[0][j]){
//what i want to do is to compare from 0 each cell of
//qual with freq with 1-9 numbers

        freq[1][j] = freq[1][j] + freq[1][j];
        //if detected a coincidence, increment the field of the row to
        //count repetitions
        //so, if qualifications[i] it's now '2' when compared to 
        //freq[0][2] (what it's 2) freq[1][2] it's now 1.  
        }
        j=i; 

        if(j>=9){ //to avoid that j be bigger than freq column size
        j=0;
        }

    }
    j=1; //skip 0
        while(j<=9){
            //j print 1-9 numbers and freq[1][j] print the number of
            //times it is repeated 
                cout << "Qualification " << j << " repeated " << freq[1][j] << " times." << endl;
            j++;
        }
}
Plàcid Masvidal
  • 130
  • 1
  • 2
  • 12
  • So you want a function to count occurrences right? https://stackoverflow.com/questions/54476881/how-to-apply-the-concept-of-counting-occurrences-on-strings-variables-in-c/54477263#54477263 – YSC Feb 01 '19 at 16:54
  • hummm... the problem it's that in my school we don't studied yet classes, the use of std:: and many other things of that code, I don't understand that code... Our homework exercise is to calculate the frequencies of and array entered by the user and we only know arrays, loops, and not much more... – Plàcid Masvidal Feb 01 '19 at 16:59
  • So you need to retag this as [tag:C]. This is not C++. You use `std::cout` though. Anyway, good luck. – YSC Feb 01 '19 at 16:59
  • Ok, it's edited. – Plàcid Masvidal Feb 01 '19 at 17:01
  • What do you get when you run your code? What is not happening? – nicomp Feb 01 '19 at 17:03
  • 1
    @YSC How can you argue that it's "not C++" if it uses `std::cout`? – Govind Parmar Feb 01 '19 at 17:06
  • @GovindParmar This is a misunderstanding: I'm saying that C++ without classes or its standard library is not C++. It is not C either since OP use `std::cout. That was my line of thinking. – YSC Feb 01 '19 at 17:09
  • @nicomp get this: https://imageshack.com/a/img924/2964/0Whdkz.png Enter the qualifications (from 0 al 10): 5 6 1 8 4 2 8 8 1 2 4 0 Qualification 1 repeated 0 times. Qualification 2 repeated 0 times. Qualification 3 repeated 0 times. Qualification 4 repeated 0 times. Qualification 5 repeated 0 times. Qualification 6 repeated 0 times. Qualification 7 repeated 0 times. Qualification 8 repeated 0 times. Qualification 9 repeated 0 times. – Plàcid Masvidal Feb 01 '19 at 17:10
  • @YSC Well, even if it's a poor subset of C++, it cannot be compiled as C, so I don't think the C tag is correct either. It's probably better for the asker to clarify (editing the question) which language is supposed to use (now I read C) and what kind of constructs they are allowed to use, beeing this an assignment. – Bob__ Feb 01 '19 at 17:18
  • I agree. The YSC writing the end of my comment was already contradicting the YSC starting the comment. I thought _"You're using `std::cout` though"_ was clear enough. My Bad. – YSC Feb 01 '19 at 17:22
  • C++ without classes is not C. The languages have evolved apart. `std::cout` will not compile in C. This should be tagged c++ – Manny_Mar Feb 01 '19 at 18:13
  • If for all digits `n`, `array[n]==n`, then just use `n` in the code. There is no need to create an array to store the ten possible digits. We already know what they are. – Tim Randall Feb 01 '19 at 19:11

1 Answers1

0

In your original code you are not checking each 'qualification' against each possible value in 'freq'.

I think something like this is what you're looking for.

#include <iostream>
using namespace std;

int main()
{
  //make an example 'qualifications'
  int SIZEQ = 5;
  const int SIZEQ = 5;
  int qualifications[SIZEQ] = {1,1,2,3,4};




  int freq[2][10]={{0,1,2,3,4,5,6,7,8,9},{0,0,0,0,0,0,0,0,0,0}};

  for (int i = 0; i < SIZEQ; i ++) // loop through each qualification
  {
    for (int j = 0; j < 10; j++)   // loop through each frequency to see if it matches
    {
      if(qualifications[i]==freq[0][j])
      {
        freq[1][j] += 1;
      }
    }

  }
  int k;
  k=1; //skip 0
  while(k<=9){
    //j print 1-9 numbers and freq[1][j] print the number of
    //times it is repeated
    cout << "Qualification " << k << " repeated " << freq[1][k] << " times." << endl;
    k++;
  }



  return 0;
}

the output should be:

Qualification 1 repeated 2 times.
Qualification 2 repeated 1 times.
Qualification 3 repeated 1 times.
Qualification 4 repeated 1 times.
Qualification 5 repeated 0 times.
Qualification 6 repeated 0 times.
Qualification 7 repeated 0 times.
Qualification 8 repeated 0 times.
Qualification 9 repeated 0 times.