Click here to Skip to main content
15,884,078 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have this code that the input is random for example :

33 32 65 55 44 pe *&44^ %123^
now I need to find which two digit number was repeaed most (and the number needs to contain digits from 1 to 9, which mean 10,30.. are not valid numbers), now what seperate between two digit number to another is every input exept for another number, in this case I want the output to be 44 then I will print:

The winner couple is: 4 blue, 4 red.
now I thought the best way to do something like this is using 2D array, that when a input is valid I will apdate the array in the sutble place, and after that I will find the maximum place in the array.
.

What I have tried:

C#
#include <stdio.h>
#include <stdlib.h>
#define N 9

void update_array(int num[N][N]);
int find_max(int b[N][N]);

int main()
{
    int i,j;
    int num[N][N];
    int maximum=0,a=0,b=0;
    for(i=0;i<N;++i)
    {
      for(j=0;j<N;++j)
      {
       num[i][j]=0;
      }
    }
    update_array(num);
    maximum=find_max(num);
    a=maximum/10;
    b=maximum%10;
    printf("The winner couple is: %d blue, %d red.",a,b);
  return 0;
}


void update_array(int num[N][N])
{
char c;
int digits = 0;
int redIndex = 0;
int blueIndex = 0;
while (scanf("%c", &c) != EOF)
{
    if (c>'0'&&c<='9')
    {
        digits++;
        c = c -'0';
        if (digits == 1)
            redIndex = c;
        else if (digits == 2)
            blueIndex = c;
        else
            continue;
    }
    else
    {
       if (digits == 2)
        {
            (num[redIndex][blueIndex])++;
        }
        else
          continue;
        digits = 0;
    }

}
}




int find_max(int b[N][N])
{
   int max = b[0][0];
   int x,y;
   int a=0,v=0,c=0;
   for (x = 0; x < N; x++)
   {
       for (y = 0; y < N; y++)
       {
           if (max < b[x][y])
           {
               max = b[x][y];
                a=x;
                v=y;
           }
       }
   }
    c=((a*10)+v);

   return c;
}

i have not tied anything yet cause i am still thinking of a way, thats why i asked for you're help!
Posted
Updated 12-Jun-16 12:07pm
v12
Comments
Sergey Alexandrovich Kryukov 11-Jun-16 18:54pm    
Excuse me, did you look at your own code after you posted it? Please click "Improve question" to see how it should be formatted; I've done it for you, but next time please make sure you write readable posts.

No, in the section "What I've tried" you did not show anything relevant. Sorry, but we don't do you homework for you. This is not a valid question. What do you mean "how?". If you have some real questions, we will gladly try to help you.

And — what ideas? The formulation of the problem is not clear, but if you have points assigned for the results of all dances, you just have to traverse the set of all pairs and/or all individuals and find the best by the criteria you have. What would be the problem here?

—SA
molan77 11-Jun-16 19:02pm    
i didn't ask you to solve my homework , i just asked for a direction to start, cause now i am haveing a blackout, just a tip \hint to give me motivation to start the code,and then i write the code and if i have then something spesific about the code i will ask.
if you don't want to help me it is ok ,just plz don't come and tell me that i asked you to solve the question for me,cause i didn't, and if i wasn't hopless i wouldn't come here and ask for a direction.
i don't know what i did wrong.
Sergey Alexandrovich Kryukov 11-Jun-16 19:19pm    
I know, I know. I never said that you asked for it or did something wrong (except formatting and not clear formulation), I just don't understand what else could help you.
I just gave you some hint. Can you use it and go ahead? If you have some particular concern, please explain it.
—SA
molan77 11-Jun-16 19:51pm    
ok, lets say i have the input:31 33 32 65 55 44 pe *&44^ %123^ 25, this shows what the judges vote , some of the votes are invalid for exampla 123,every two digit numbers that are seperated from other numbers by anything exept numbers consider valid foe example: *&44^ considered valid input and (member 4 from the blue team and the red team gets a point).
now i want to do 2D array that will apdate the ij place in every vote, then i will chick whick two digit number repeated most and this couple will be the winner...
i hope you understand (english is not my first language so it is dificult for me to explain)
my problem is how to do a loop that will run on the input and chicks where there is two digit number...
Sergey Alexandrovich Kryukov 11-Jun-16 20:03pm    
You know, you have to formulate everything mathematically strictly.
You have to understand that an example is not the definition.
31 — what is that vote for? 33? What is pe *&44^ %123^?
Yes, the set of pairs can be represented as the array of N x M (N = M = 9 in your case). Fill in this array with the data you have. Then you can traverse all this array; and can traverse your red and blue arrays.
What are your concerns about it?
—SA

You could use two arrays, one for blue and one for red with 9 elements each.

C++
int redVotes[9];
int blueVotes[9];

Make sure to initialize both arrays with 0. (Hint: Use memset)

When you have filtered out the valid input value pairs, you split each pair into one high (blue) and one low (red) value.

[UPDATE]
One problem with the input approach you have chosen is that you are supposed to ignore numbers with 3 or more digits. This requires that you either can predict the future or have some sort of rollback functionality.

Maybe something like this. Just beware that I wrote this code here in this editor and it is not tested at all.
C++
char c;
int digit = 0;
int digits = 0;
int redIndex = 0;
int blueIndex = 0;
while (scanf("%c", &c) != EOF)
{
    if (isdigit(c))
    {
        digits++;
        digit = c -'0';  // Or use atoi(&c); to convert the character to an int
        if (digits == 1)
            redIndex = digit;
        else if (digits == 2)
            blueIndex = digit;
    }
    else
    {
        // This should make sure you only accept 2 digit numbers
        if (digits == 2)
        {
            redVotes[redIndex]++;
            blueVotes[blueIndex]++;
        }
        digits = 0;
    }
}


This part is not necessary anymore
C++
int input = 35; // This is probably an array as well.
int redValue = 5;
int blueValue = 3;

(Hint: Use modulus for the red value and integer division for the blue value)

Then you use the each indvidual value as an index in each of the two arrays and increment the value for that index.
C++
redVotes[redValue]++;
blueVotes[blueValue]++;


Then it is just a matter of finding the max value of each array, combine the values and you have a winner.
If you cannot find a unique max value for one of the arrays, the result is indecisive.

So, that should get you started. I have not solved everything for you, just showed you one way of many to do it.
 
Share this answer
 
v2
Comments
molan77 11-Jun-16 21:30pm    
hi george, i thought that ding 2D array would make it easier :\
what do you think of the way i thought to solve it ?
George Jonsson 11-Jun-16 21:36pm    
Not sure why you think a 2D array would be easier, but if you really want to have it that way just replace
int redVotes[9];
int blueVotes[9];
with
votes[9][9]; // But use your defined N instead of the hard coded numbers

You still need to find the maximum for each "column" in the matrix.
molan77 11-Jun-16 21:36pm    
my problem is when i run at the input , when i get for example to a number i want to chick if the following input is also a number and the folloing one is not, so then i will simply save the first number and the second number and then update the suited place in the array , and continue , then i can search the array and find which number apeared most..
i feel like i can't explain myself good :(
George Jonsson 11-Jun-16 22:37pm    
Well, next time be sure to explain what your real problem is. Your question and your code doesn't really match.
(And I usually don't read the code because it is too messy)
molan77 12-Jun-16 17:02pm    
hi george, i apdated the code and the question, can you chick it if you have time ^.^
Advice:
Don't ask questions this way. Never mix unrelated problems.
- either you want help with invalid inputs.
- either you want help with how to tell who is the winner.
Mixing both is just confusing for everyone.

All this because your program will reflect that!
You have to program 1 part that read votes and filter invalid inputs; a,d another part that will receive only valid inputs and deal with the winner.
 
Share this answer
 
Comments
molan77 11-Jun-16 22:14pm    
i know i made a mess and i am sorry , but i i saied what my problem is couple of times but i feel like i am explaining myself good thats why no one can help me lol it is my foult, i know how to find the winner later but my problem is how to chick if the input is valid , and update the array... i am thinking of doing onther array that will save the two numbers maybe , i will continue this tomorrow .
Patrice T 11-Jun-16 22:25pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

Read again your question: not obvious that the problem is the valid/invalid input.
molan77 12-Jun-16 17:40pm    
can you plz chick my updated code if you have time ,
and a question, when i update the code, do you guys se that or i need to tell you? i honestly don't know !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900