number of non same elements

This is a discussion on number of non same elements within the C++ Programming forums, part of the General Programming Boards category; hi this program is for counting number of nons ame elements in an array of size 5 i think this ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    41

    Arrow number of non same elements

    hi
    this program is for counting number of nons ame elements in an array of size 5
    i think this program is specific for array of size 5 this is not general
    i want to make a general program, means by changing just 1 or 2 values in the code i can use it for higher size arrays here is my code
    Code:
    # include <iostream>
    # include <conio.h>
    # include <windows.h>
    using namespace std;
    int main ()
    {
    	int a[10];
    	
    	char push_key='#';
    	while (push_key!='@')
    	{
    	double n=0;
    	for (int i=0;i<5;i++)
    	{
    		cout<<"enter number";
    		cin>>a[i];
    	
    	}//end for
    	for (int i=0;i<5;i++)
    	{
    		for (int j=i+1;j<5;j++)
    		{
    		if (a[i]==a[j])
    			{
    			
    				n++;
    		
    				}//end if
    		
    		}//end for
    		if (n==4)
    		{
    			break;
    		
    		}//end if
    		
    		}//end for
    	if (n==4){
    	cout<<"total non same elements are:"<<4-n<<endl;
    	cout<<endl<<endl;
    		}//end if
    	if (n<4) 
    	{
    	cout<<"total non same elements are:"<<5-n<<endl;
    	cout<<endl<<endl;
    	
    	}//end if
    	if (n>4) 
    	{
    any help will b appreciated thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by rafay_07
    this program is for counting number of nons ame elements in an array of size 5
    What exactly do you mean by "number of non-same elements"? For example, suppose we have an array consisting of two 1s and three 2s. How many elements are "non-same"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    let me explain
    suppose i have an array of size 5 and i enter these number
    1 2 2 4 5
    here 1, 4,5 are not same so i say number of non same elements is 3 while number of same elements is 2
    so i want to display number of non same elements in the array

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    hmm... well, if I understand this right, I would suggest that you make use of a std::map<int, size_t>. You just insert into the map and increment the value (i.e., map the elements to their counts). When you are done, you iterate over the map. All keys for which the values are 1 are "non-same".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    18
    Let an integer 'x' equal some number of interest, and let integer 'k' equal zero. Then in a loop from j = 0 to array.size()-1 perform a check against each (array[j] == x); If true do nothing, if not true increment k by one. When the loop completes 'k' will represent the number of elements which are not the same as some given value 'x'.

    I figure that's not a complete solution for what you're looking to do, but it is a start.

    In a problem like this, especially if you're new to algorithms/math/programming, what I recommend you do is you write out the steps that are necessary to complete a task, make it work on paper. Then use what you know of some programming language to implement those steps for a program to execute. Sometimes it helps to think 'out loud' on paper before writing code, that's usually a good way of figuring these things out on your own.
    Last edited by syneii; 12-07-2010 at 12:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. number of elements in double linked list
    By cnu_sree in forum C++ Programming
    Replies: 4
    Last Post: 01-25-2008, 12:18 PM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 01:56 PM
  3. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 02:03 AM
  4. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 02:53 PM
  5. letting user input number of elements in array
    By iamthejake2000 in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2005, 01:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21