Thread: working with arrays

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    working with arrays

    I am writing a program that creates an int array, takes ints from the user, and determines the min, max and average of all values. This part I can do. What I am having trouble with is that the values entered can only be positive. So if the user enters a negative number I have to send an error message to the screen saying int must be positive, which I have done already. I can't figure out how to keep from counting that value in my counter and not include the negative number in my calculations for the minimum.

    Here is the code:
    Code:
     #include <iostream>
    
     using namespace std;
    
     int main()
     {
    	 int nums[100];
    	 int i, max, min, total =0, count = 1;
    	 double avg;
    
    	 cout << "Enter positive interger numbers (or -1 to quit): " << endl;
    	 cin >> nums[0];
    	 if (nums[0] == -1)
    	 	 	{
    	 			cout << "Bye.";
    	 			return 0;
    	 		}
    	 	if (nums[0] < -1)
    	 	cout << "********ERROR SKIPPED ENTRY********" << endl << "Number must be positive";
    
    	 max = nums[0];
    	 min = nums[0];
    	 total = nums[0];
    
    while (i > -1)
    {
    	 for (i = 1; i < 100; i++)
    	{
    		cin >> nums[i];
    
    	if (nums[i] < -1)
    	 {
    		cout << "********ERROR SKIPPED ENTRY********" << endl << "Number must be positive";
    		count = count -1;
    		total = total - i;
    	 }
    
    	 if (nums[i] == -1)
    	 {
    	 	cout << "Read in " << i << " values." << endl;
    	 	cout << "Minimum value: " << min << endl;
    		cout << "Maximum value: " << max << endl;
    		cout << "Average: " << (total +1) / i << endl;
    		cout << count << endl;
    		cout << total;
    	 	return 0;
    	}
    	 else
    	{
    		if (nums[i] > max)
    		 max = nums[i];
    
    		if (nums[i] < min)
    		 min = nums[i];
    
    		 count ++;
    		 total = nums[i] + total;
    	}
    }
    }
     return 0;
     }
    Thanks for any help.

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You could do a do/while loop like so:
    Code:
    do
    {
        cin>>nums[i];
    } while (nums[i]<-1);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  2. Merging two arrays.
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-21-2004, 07:00 AM
  3. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM
  4. arrays arrays arrays....
    By Jan79 in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2002, 07:16 AM
  5. working with strings arrays and pointers
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-30-2002, 08:32 PM