445,897 Members | 1,969 Online
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 #include  using namespace std; #include           int find_maximum(int a[], int n)  {      int maximum=a[0];      for (int i=0;imaximum)              maximum=a[i];      }      return (maximum);  }          int find_value(int a[], int n, int target) {     bool NotFound = true;     int i = 0;       while(i < n && NotFound)     {         if(target != a[i])                 i++;         else             NotFound = false;     }       if( NotFound == false )         return i;     else         return -1; }     int main()  {       int a[1000];       // Declare an array of 1000 ints     int n = 0;     int target=0;     int Q= sizeof (a) / sizeof (int);   // Number of values in a.       while (cin >> a[n])      {         n++;     }       int sum = 0;       // Start the total sum at 0.     for (int i=0; i>target;     if(i == -1)         cout << target << " was not found in the collection\n\n";     else     {         cout << target << " is at the " << i+1;         if( i == 0 )             cout<< "st position of the collection\n\n";         else if( i == 1 )             cout<< "nd position of the collection\n\n";         else if( i == 2 )             cout<< "rd position of the collection\n\n";         else             cout<< "th position of the collection\n\n";     }       return 0; }   Thanks alot Nov 2 '06 #1
5 Replies

 P: 9 Expand|Select|Wrap|Line Numbers #include  using namespace std; #include           int find_maximum(int a[], int n)  {      int maximum=a[0];      for (int i=0;imaximum)              maximum=a[i];      }      return (maximum);  }            int find_value(int a[], int n, int target) {     bool NotFound = true;     int i = 0;       while(i < n && NotFound)     {         if(target != a[i])             i++;         else             NotFound = false;     }       if( NotFound == false )         return i;     else         return -1; }     int main()  {       int a[1000];       // Declare an array of 1000 ints     int n = 0;     int target=0;     int Q= sizeof (a) / sizeof (int);   // Number of values in a.       while (cin >> a[n])      {         n++;     }       int sum = 0;       // Start the total sum at 0.     for (int i=0; i>target;     int i = find_value(a, Q, target);     if(i == -1)         cout << target << " was not found in the collection\n\n";     else     {         cout << target << " is at the " << i+1;         if( i == 0 )             cout<< "st position of the collection\n\n";         else if( i == 1 )             cout<< "nd position of the collection\n\n";         else if( i == 2 )             cout<< "rd position of the collection\n\n";         else             cout<< "th position of the collection\n\n";     }       return 0; }     edited code. Thanks Nov 2 '06 #2

 Expert 100+ P: 1,510 your input loop Expand|Select|Wrap|Line Numbers     while (cin >> a[n])      {         n++;     }   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     while (cin >> a[n])      {         n++;     }   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     while (cin >> a[n])      {         n++;     }     cin.clear();               // clear error condition     cin.ignore(1000,'\n');     // discard any characters to newline   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     while (cin >> a[n])      {         n++;     }     cin.clear();               // clear error condition     cin.ignore(1000,'\n');     // discard any characters to newline   Thanks it's works perfectly now, and runs good :P Nov 2 '06 #6