Thread: Array sorting problems

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    89

    Array sorting problems

    I have two array x and y. I want to find out duplicate value store in array y

    I wrote program and I can find duplicate value

    Code:
     #include<stdio.h> 
    int main(void) {
      
      int i = 0, j = 0;
      
      int x[5] = { 1, 2, 4, 3,  5 };
      
      int y[5] = { 10, 3, 13, 2, 17 };
      
      int temp[5];
      
      for ( i = 0; i < 5; i ++)
      {
    	  for ( j = 0; j < 5; j ++)
    	  {
    		if ( x[i] == y[j])
    		{
    			printf("Number  %d found \n", x[i] );
    		}	
    
    
    	  }
      }
    	
    	return 0;
    }
    program output

    Code:
     Number  2 found
    Number  3 found
    I am trying to solve following problem where duplicate number 3 is repeating two times

    Code:
       int x[5] = { 1, 2, 4, 3,  5 };  
      int y[5] = { 10, 3, 3, 2, 17 };
    I don't understand how to make a logic that find how many time number repeats in array ?

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    It often helps to write down what you want the output to be.

    That will at least guide you do discover what information you have, and what information you need to compute.

    Code:
     1 appears 0 time(s).
     2 appears 1 time(s).
    ...
    You could then change your program to print the output with a question mark where the count should be, and that should hopefully indicate where you will need to make changes.

  3. #3
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    You should be able to create a counter inside your outer loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting problems.
    By Sedvan in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2008, 01:13 AM
  2. Sorting Problems
    By WolfPack in forum C Programming
    Replies: 2
    Last Post: 12-04-2002, 10:22 AM
  3. Sorting Problems
    By kas2002 in forum C Programming
    Replies: 11
    Last Post: 10-23-2002, 05:28 PM
  4. Array sorting problems
    By cazil in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2002, 01:36 PM

Tags for this Thread