Matching Numbers in Arrays

This is a discussion on Matching Numbers in Arrays within the C++ Programming forums, part of the General Programming Boards category; Code: for(int j = 0; j <=5; j++) { if(Un[j]==Wn[j]) { counter++; cout << Un[j] << " "; } } ...

  1. #1
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369

    Matching Numbers in Arrays

    Code:
    for(int j = 0; j <=5; j++)
    {
    	if(Un[j]==Wn[j])
    	{
    		counter++;
    		cout << Un[j] << " ";
    	}
    }
    For some reason the code above doesn't work in my program Any solutions?
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    what happens? What are contained in all those vars? How bout postin it all (at least post the code with the declarations)
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  3. #3
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    The beef:

    Code:
    int Un[6] = {0}, Wn[6] = {0};
    
    Input:
    
    cout << endl << "Please Enter Six Consecutive Numbers Ranging from 1-53:\n" << endl;
    cin >> Un[0];
    
    for(int k = 1; k <= 5; k++)
    {
    	int n = k - 1;	// Previous value
    
    	if(Un[n] >= 1 && Un[0] <= 53)	// If it's valid
    	{
    		cin >> Un[k];	// Get next value
    	}
    	else
    	{
    		cout << "\a";
    		goto Input;	// Else prompt again
    	}
    }
    
    for(int i = 0; Wn[i] == 0; i++)	// Fill Wn[] with random values
    {
    	Wn[i] = rand()%53;
    }
    
    cout << endl << endl << "Matching Numbers: ";
    
    int counter = 0;	// Keep track of matching numbers
    
    for(int j = 0; j <=5; j++)
    {
    	if(Un[j]==Wn[j])
    	{
    		counter++;
    		cout << Un[j] << " "; // This doesn't show
    	}
    }
    
    cout << endl << endl
    
    if(counter==0)	// These don't show either
    {
    	//.....
    }
    else if(counter==1)
    {
    	//.....
    }
    else if etc etc...
    Last edited by abrege; 11-24-2002 at 02:35 AM.
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    
    int main(){
    
    	using namespace std;
    	
    	srand(time(NULL));//first randomise
    	
    	const int nArraySize = 6;
    	int Un[nArraySize] = {0}, 
    		Wn[nArraySize] = {0};
    		
    	cout << endl << "Please Enter Six Consecutive Numbers Ranging from 1-53:\n" << endl;
    	
    	for(int i = 0;i < nArraySize;++i){
    		cin >> Un[i];
    		if(Un[i] > 53){//if over 53
    			cout << "Not under 54!" << endl;
    			--i;//lower count
    		}
    	}
    	
    	
    	for(int i = 0;i < nArraySize;++i)
    		Wn[i] = rand()%54;//between 0 and 53
    		
    	cout << endl << endl << "Random Numbers: ";
    	
    	for(int i = 0;i < nArraySize;++i)
    		cout << Wn[i] << " ";
    		
    	cout << endl << endl << "Matching Numbers: ";
    
    	int counter = 0;// Keep track of matching numbers
    	
    	for(int i = 0;i < nArraySize;++i)
    		for(int j = 0;j < nArraySize;++j)//nested loop
    			if(Un[i]==Wn[j]){
    				counter++;
    				cout << Un[i] << " ";
    		}
    }

  5. #5
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Thanks, it all works now!
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matching numbers
    By kirksson in forum C Programming
    Replies: 7
    Last Post: 07-23-2008, 01:51 PM
  2. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  3. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  4. Doing multiplication using numbers in arrays
    By neandrake in forum C++ Programming
    Replies: 17
    Last Post: 11-05-2004, 10:07 AM
  5. using arrays to sort numbers
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:14 PM

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