Thread: comparing a number to an array

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    number == values[i]
    - i have tried doing that but it gives me error "="
    What is the error message?

    You can break from the loop, return from the function, or base the loop condition on a flag (i.e., some boolean variable) that is set when a match is found.
    - i have no idea how to do that
    Read the tutorials on loops and functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User sndpchikane's Avatar
    Join Date
    Mar 2008
    Posts
    5

    TO find whether value is unique

    Hi

    Here is program that i have written to find whether value entered in array is unique or not hope this will help you.
    Code:
    /*
      Name: Sandeep Chikane
      Copyright: 18/03/08 11:43
      Author: Sandeep Chikane
      Date: 18/03/08 11:43
      Description: Program to find unique values to enter in array
    */
    
    
    
    # include<stdio.h>
    # include<conio.h>
    
    
    //Prototype
    
    int unique(int [],int,int);
    
    main()
    {
          int i,j,k,arr[5]={0,0,0,0,0};
    
          int val,valid;
          
                  
          printf("Enter values in array\n");
          
          i=0;
          
          do
          {
              scanf("%d",&arr[i]);
              
              val=arr[i];
              
              valid=unique(arr,val,i);
          
              if(valid==1)
                             printf("\nValue Entered is repeated\n");
              else
                             printf("\nValue Entered is unique\n");
     
                             
              i=i+1;
          
          }
          while(i<5);
          
          printf("\nenter any key to exit");
          getch();
    }
    
    int unique(int arr[],int val,int size)
    {
        int i,flag=0;
    
        for(i=0;i<size;i++)
        {
                         if(arr[i]==val)
                         {
                                        flag=1;
                                        break;
                         }
                         else
                         {
                             flag=0;
                         }
        }
        
        if(flag==1)
        {
                   return(1);
        }
        else
        {
                   return 0;
        }          
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 02-22-2009, 05:17 PM
  2. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  3. finding the element number in an array
    By tommy69 in forum C Programming
    Replies: 7
    Last Post: 04-02-2004, 04:26 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM