//This function prints all scores, but if a score occurs more than once, it only prints it one time

void scores_once(int x[20], int size, int new_array[20])
{
    for(int i=0; i<size; i++)
       {
             new_array[i]= -1;
       }
       
     new_array[0]= x[0];
     
     cout<<new_array[0]<<endl;
     
    for(int j=1; j<size; j++)
          {
                  
               for(int k=0; k<size; k++)
                 {  
                       if (x[j]!=new_array[k])
                       new_array[k+1]=x[j];  
                 }               
     for (int l=0; l<size; l++)
         {
              cout<<new_array[l]<<endl;
         }
         
}

Recommended Answers

All 4 Replies

Syntactically your code is alright , but there is a logical error that you will have to figure out.

The basic algorithm would be like this

Take a value from the x List. then check if the value is present in all the array-spaces in the new_array. If it is not present then assign the value of the variable into the new_array.

But as far as your approach is concered you are doing the following.

Take a value from the x List. then check if the value is present in One of the array-spaces in the new_array. If it is not present then assign the value of the variable into the new_array.

The problem occurs when you are assigning the value to the variables only after checking a single value but not all of the values in the array.

I thought we checked all the values in the array? how do I fix this

1) you could create a vector.
2) Initialize it with the array content
3) use std::sort on the vector
4) use std::unique on vector
5) Then print out the vector

Use something like a boolean flag.
I will not give you the code, but the method of checking.

bool flag=false;
 for(check throughout the array)
  { if(arrayelement==our_element)
   {
     flag=true;
     break;
    }
   }
if(flag==false)
{
  assign our_element to a position in the array
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.