Thread: somebody help me with this array program

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    6

    Unhappy somebody help me with this array program

    hey guys..ok i need to write a program that stores up to 25 numbers from the user and stores them in an array. when the user enters a number below 0, the program ends. once all the numbers entered by the user, the program needs to find and delete all repeats, leaving only unique numbers (those that are not repeated numbers). it also needs to print the total number of numbers entered. i have this much entered from another program, and i only need to change some things. im really stuck and i really need help!
    sample:
    enter a number: 6
    enter a number: 17
    enter a number: 4
    enter a number: 6
    enter a number: 14
    enter a number: 4
    The numbers entered are: 6 17 4 6 14 4
    The unique numbers are: 6 17 4 14

    Oh man if you guys can help me please! i've been stuck on this program for over a week, im really bad at this programming stuff...

    heres the program:
    Code:
     #include <iostream>
    using namespace std;
    
    int main() {
    
    	const int MAX_SIZE = 20;
    	int numbers[MAX_SIZE];
    	int count = 0;
    	int numberEntered;
    
    	do {
    
    		cout << "Enter Number: ";
    		cin >> numberEntered;
    
    		if (numberEntered != -1) {
    			numbers[count++] = numberEntered;
    		}
    	}while (numberEntered != -1 && count < MAX_SIZE);
    
    	for (int i = 0; i < count; i++) {
    		cout << "--------------------------------" << endl;
    		//cout << "Checking " << numbers[i] << endl; - debugging line
    		if (numbers[i]%2 ==0) { //if even
    
    			cout << numbers[i] << " is even" << endl;
    
    			int deleteHere = i;
    			for (int j = deleteHere; j < count-1; j++) {
    				numbers[j] = numbers[j+1];
    			}
    			i--;
    			count--;
    			//cout << "Deleting " << numbers[i] << endl; - debugging line
    		for (int k = 0; k < count; k++) {
    		cout << numbers[k] << " ";
    		}
    		cout << endl;
    		}
    	}
    
    	for (int i = 0; i < count; i++) {
    		cout << numbers[i] << " ";
    	}
    	cout << endl;
    
    }
    
    //for hmwk 6: if x[i] == x[j]
    				//delete x[j]

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    What does the program do ? Is there a compiler error ? If not, does it crash ? Do you get unexpected results ? What are those results ? What do you think those results might mean ? If there are compiler errors, what are they ? Where are they ? What does the error mean ?

    You should learn how to post questions. We don't have that much time to spend on your question but if you provide all the details (and more) that we need then maybe the answer will become obvious to us and we can help you more.

    I scanned your code *really* quickly and noticed that you had numberEntered != -1 in your while condition. Is -1 the only possible negative number ? Think of negative numbers as numbers below 0. How would you express that in code ? Or even in maths if you will.

  3. #3
    Registered User SpaceCadet's Avatar
    Join Date
    Oct 2006
    Posts
    23
    This does not do what your code does. It does meet your requirements.

    Code:
    int main()
    {
        const int MIN=0;
        const int MAX=25;
        int numbers[MAX];
        int number=MIN;
        ostringstream sequence;
    
        // Initialise a count of all numbers between 1 and MAX
        for (int index=MIN; index<MAX; index++)
        {
            numbers[index]=MIN;
        }
    
        // Read ahead
        cout << endl << "Enter a number between 1 and " << MAX " : ";
        cin >> number;
        sequence << number << " ";
    
        // Decrement number to align with array index
        while ((--number>=MIN) && (number<MAX))
        {
            numbers[number]++;
            cout << endl << "Enter a number between 1 and " << MAX << " : ";
            cin >> number;
            sequence << number << " ";
        }
    
        // Display a count of all numbers entered between MIN and MAX
        for (int index=MIN; index<MAX; index++)
        {
            cout << index+1 << " was entered " << numbers[index] << " times." << endl;
        }
    
        cout << "Number entry sequence "<< sequence.str() << endl;
    
        cout << "You entered " << number << " to exit " << endl;
    
        return 0;
    }
    All questions are easy. The answers cause the problem...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Array Program
    By alex1067 in forum C Programming
    Replies: 5
    Last Post: 04-15-2008, 06:26 AM
  3. help with small program. array issue
    By InvariantLoop in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 12:26 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM