By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

Mayday....cant fix my program:(

P: 9
Hello peeps.

Well i have tried runing the folwoing program several times, and i cant figure out what i am doing wrong.
The goal of this is to input values for an array, find the maximum and the do a linear search for a given number.
But i cant seem to input the number anywhere.


Any kind of input would help me out alot.
Thanks ;)


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. #include <iomanip>
  4.  
  5.  
  6.  
  7.  
  8.  int find_maximum(int a[], int n)
  9.  {
  10.      int maximum=a[0];
  11.      for (int i=0;i<n;i++)
  12.      {
  13.          if(a[i]>maximum)
  14.              maximum=a[i];
  15.      }
  16.      return (maximum);
  17.  }
  18.  
  19.  
  20.  
  21.  
  22.  int find_value(int a[], int n, int target)
  23. {
  24.     bool NotFound = true;
  25.     int i = 0;
  26.  
  27.     while(i < n && NotFound)
  28.     {
  29.         if(target != a[i])    
  30.             i++;
  31.         else
  32.             NotFound = false;
  33.     }
  34.  
  35.     if( NotFound == false )
  36.         return i;
  37.     else
  38.         return -1;
  39. }
  40.  
  41.  
  42. int main() 
  43. {
  44.  
  45.     int a[1000];       // Declare an array of 1000 ints
  46.     int n = 0;
  47.     int target=0;
  48.     int Q= sizeof (a) / sizeof (int);
  49.  
  50. // Number of values in a.
  51.  
  52.     while (cin >> a[n]) 
  53.     {
  54.         n++;
  55.     }
  56.  
  57.     int sum = 0;       // Start the total sum at 0.
  58.     for (int i=0; i<n; i++) 
  59.     {
  60.         sum = sum + a[i];  // Add the next element to the total
  61.     }
  62.  
  63.     cout << sum << endl;
  64.     cout<<"The maximum value for this array is  "<<find_maximum(a,n)<<endl;
  65.     cout<<"Enter the value you wish to find it's position in the array";
  66.     cin>>target;
  67.     if(i == -1)
  68.         cout << target << " was not found in the collection\n\n";
  69.     else
  70.     {
  71.         cout << target << " is at the " << i+1;
  72.         if( i == 0 )
  73.             cout<< "st position of the collection\n\n";
  74.         else if( i == 1 )
  75.             cout<< "nd position of the collection\n\n";
  76.         else if( i == 2 )
  77.             cout<< "rd position of the collection\n\n";
  78.         else
  79.             cout<< "th position of the collection\n\n";
  80.     }
  81.  
  82.     return 0;
  83. }
  84.  

Thanks alot
Nov 2 '06 #1
Share this Question
Share on Google+
5 Replies


P: 9
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. #include <iomanip>
  4.  
  5.  
  6.  
  7.  
  8.  int find_maximum(int a[], int n)
  9.  {
  10.      int maximum=a[0];
  11.      for (int i=0;i<n;i++)
  12.      {
  13.          if(a[i]>maximum)
  14.              maximum=a[i];
  15.      }
  16.      return (maximum);
  17.  }
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  int find_value(int a[], int n, int target)
  24. {
  25.     bool NotFound = true;
  26.     int i = 0;
  27.  
  28.     while(i < n && NotFound)
  29.     {
  30.         if(target != a[i])
  31.             i++;
  32.         else
  33.             NotFound = false;
  34.     }
  35.  
  36.     if( NotFound == false )
  37.         return i;
  38.     else
  39.         return -1;
  40. }
  41.  
  42.  
  43. int main() 
  44. {
  45.  
  46.     int a[1000];       // Declare an array of 1000 ints
  47.     int n = 0;
  48.     int target=0;
  49.     int Q= sizeof (a) / sizeof (int);
  50.  
  51. // Number of values in a.
  52.  
  53.     while (cin >> a[n]) 
  54.     {
  55.         n++;
  56.     }
  57.  
  58.     int sum = 0;       // Start the total sum at 0.
  59.     for (int i=0; i<n; i++) 
  60.     {
  61.         sum = sum + a[i];  // Add the next element to the total
  62.     }
  63.  
  64.     cout << sum << endl;
  65.     cout<<"The maximum value for this array is  "<<find_maximum(a,n)<<endl;
  66.     cout<<"Enter the value you wish to find it's position in the array\n";
  67.     cin>>target;
  68.     int i = find_value(a, Q, target);
  69.     if(i == -1)
  70.         cout << target << " was not found in the collection\n\n";
  71.     else
  72.     {
  73.         cout << target << " is at the " << i+1;
  74.         if( i == 0 )
  75.             cout<< "st position of the collection\n\n";
  76.         else if( i == 1 )
  77.             cout<< "nd position of the collection\n\n";
  78.         else if( i == 2 )
  79.             cout<< "rd position of the collection\n\n";
  80.         else
  81.             cout<< "th position of the collection\n\n";
  82.     }
  83.  
  84.     return 0;
  85. }
  86.  
  87.  

edited code.


Thanks
Nov 2 '06 #2

Expert 100+
P: 1,510
your input loop
Expand|Select|Wrap|Line Numbers
  1.     while (cin >> a[n]) 
  2.     {
  3.         n++;
  4.     }
  5.  
keeps looping while it reads valid int values - enter a non int and it will terminate, e.g. type
1 2 3 4 5 6 7 8 x

and input will stop at x
Nov 2 '06 #3

P: 9
your input loop
Expand|Select|Wrap|Line Numbers
  1.     while (cin >> a[n]) 
  2.     {
  3.         n++;
  4.     }
  5.  
keeps looping while it reads valid int values - enter a non int and it will terminate, e.g. type
1 2 3 4 5 6 7 8 x

and input will stop at x
thanks for the input mate but the thing is :
you can either do that or do a CtrlD then enter, but that aint a problem, what is bothering me is the fact that i cant enter a target value to do the linear serch in the array !!!

thanks for the help :)
Nov 2 '06 #4

Expert 100+
P: 1,510
[if the loop exits on an error condition you need to clear the error and remove the faulty characters, e.g.
Expand|Select|Wrap|Line Numbers
  1.     while (cin >> a[n]) 
  2.     {
  3.         n++;
  4.     }
  5.     cin.clear();               // clear error condition
  6.     cin.ignore(1000,'\n');     // discard any characters to newline
  7.  
Nov 2 '06 #5

P: 9
[if the loop exits on an error condition you need to clear the error and remove the faulty characters, e.g.
Expand|Select|Wrap|Line Numbers
  1.     while (cin >> a[n]) 
  2.     {
  3.         n++;
  4.     }
  5.     cin.clear();               // clear error condition
  6.     cin.ignore(1000,'\n');     // discard any characters to newline
  7.  
Thanks it's works perfectly now, and runs good :P
Nov 2 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.