Thread: array query

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    75

    Question array query

    I'm writing this program right now where the user inputs integers with a limit of 100 integers and entering -999 to denote the end of the list. I'm having some trouble... it seems like the way that I have it written now, the program must read 100 integers no matter what. But I don't know what to change. I tried flipping around my "while" and "for" statements, but then that gave me some kind of internet explorer error and shut down. Anyways, if anyone can take a look and see what I have going wrong here, let me know. I'd really appreciate it.

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    const int sentinel = -999;
    void selectionSort(int number[], int length);
    
    int main()
    {
    	int number[100];
    	int index;
    
    	cout << "Enter a maximum of 100 positive integers ending with " << sentinel << endl;
    	
    	for (index = 0; index < 100; index++) 
    	{
    		while (number[index] != sentinel)
    		{
    			cin >> number[index];
    		}
    	}
    
    	selectionSort(number, 100);
    	
    	for (index = 0; index < 100; index++)
    		cout << number[index] << endl;
    
    
    	return 0;
    }
    
    void selectionSort(int number[], int length)
    {
    	int index;
    	int smallestIndex;
    	int minIndex;
    	int temp;
    
    	for (index = 0; index < length - 1; index++)
    	{
    		smallestIndex = index;
    		for (minIndex = index + 1; minIndex < length; minIndex++)
    			if (number[minIndex] < number[smallestIndex])
    				smallestIndex = minIndex;
    
    		temp = number[smallestIndex];
    		number[smallestIndex] = number[index];
    		number[index] = temp;
    	}
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Try changing (as a start):
    Code:
    while (number[index] != sentinel)
    To:
    Code:
    if (number[index] != sentinel)
    Of course, it makes more sense to stop the loop early when you get a sentinel value:
    Code:
    for ( index = 0; index < 100; index++ ) {
      cin>> number[index];
    
      if ( number[index] == sentinel )
        break;
    }
    Then index will be the number of valid items in the array, or you can use the sentinel for loop termination.
    Last edited by Prelude; 07-24-2005 at 03:22 PM.
    My best code is written with the delete key.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And then change this
    Code:
    selectionSort(number, 100);
    to
    Code:
    selectionSort(number, index);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    OK... I've done some revising. But there's still something wrong right now. If someone could copy my code into their compiler and see what is happening when you execute it maybe you could tell me what I'm still doing wrong. Something else that I'm supposed to be doing in this program is counting how many times numbers appear. I'm pretty sure that I need to use like a binary search or something, but I'm not sure how to write and keep track of it. Help!

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    const int sentinel = -999;
    void selectionSort(int number[], int length);
    
    int main()
    {
    	int number[100];
    	int index;
    
    	cout << "Enter a maximum of 100 positive integers ending with " << sentinel << endl;
    	
    	for (index = 0; index < 100; index++) 
    	{
    		if (number[index] != sentinel)
    			cin >> number[index];
    		
    		if (number[index] == sentinel)
    			break;
    	}
    
    	selectionSort(number, index);
    	
    	cout << "Number     Count";
    	for (index = 0; index < 100; index++)
    		cout << number[index] << endl;
    
    
    	return 0;
    }
    
    void selectionSort(int number[], int length)
    {
    	int index;
    	int smallestIndex;
    	int minIndex;
    	int temp;
    
    	for (index = 0; index < length - 1; index++)
    	{
    		smallestIndex = index;
    		for (minIndex = index + 1; minIndex < length; minIndex++)
    			if (number[minIndex] < number[smallestIndex])
    				smallestIndex = minIndex;
    
    		temp = number[smallestIndex];
    		number[smallestIndex] = number[index];
    		number[index] = temp;
    	}
    }

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    for (index = 0; index < 100; index++) 
    	{
      if (number[index] != sentinel)
      cin >> number[index];
    
      if (number[index] == sentinel)
      break;
    	}
    The logic here could be cleaned up a bit, though what you have is valid.

    When that section is done, index equals the number of ints put into number. Index will be valid with values of 0 to 100; inclusive.

    Then, in the second for loop you only want to loop as many times as the actual number of elements in the array, not the potential maximal number of elements in the array. So change the variable int the loop to something like i or actual or whatever, and terminate when i == index, not when index == 100, say:

    for(int i = 0; i < index; ++i)
    You're only born perfect.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Oh, duh! Thanks for pointing that out. So now, as my program stands, whatever integers I input, they are sorted and output in ascending order. But I don't want the numbers that are repeated to be output multiple times. I need to be able to read the multiple numbers and put the count of how many of each number was input in the output. Does that make sense?

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You could make an array of 101 ints and initialize each element to zero. Then read through the original array using a loop. Increment the value of the element whose index equals the value of the int in the original array by one each time through. Some of the values may remain zero, so if you only want to output those that are present at least once in the original array, you'll have to evaluate each element before outputting.


    Alternatively you could use a map with two ints, the first being the int value in the original array, and the second being the number of times that int appears in the original array.

    I'm sure there are others to do this, too. Give it your best shot and repost when needed.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM