473,396 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Mayday....cant fix my program:(

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
5 1629
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
horace1
1,510 Expert 1GB
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
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
horace1
1,510 Expert 1GB
[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
[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

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

Similar topics

1
by: otupia | last post by:
Exercise : Given the diagram below. Transform the diagram into C++ program. Class Grade Private members :char letter;float score; Public Members :void SetScore( float );float GetScore(...
2
by: confusedandinsane | last post by:
I am trying to write this short program that will calculate, (1) the cost for hotel (45 a night), cost for gas (1.67 a gallon) and total cost. The car is going to be used for a total of 200 and it...
1
by: Nagaraj | last post by:
hi all, I have simple basic c++ program "hello world" which i cant compile on linux system. I have given extension as .C. please tell me how to compile C++ programs on Linux.
12
by: lalou89 | last post by:
Develop a simple text editor program. The program will show the user a menu of choices and will act according to his choice. Use functional decomposition to break the system into small functions that...
5
by: chevon1920 | last post by:
I am trying to do my assignment but I cant figure out how to get 8 data points per line to print to a file. Here is the assignment 1. Program asks the user to enter an odd number as a BASE,...
1
by: veer | last post by:
hi i made program which calculates the time of every user works in particular SQL server and the exe of this program works fine only on those system which have vb6.0 installed but when i run this...
13
by: PetterL | last post by:
I writing a program where I read menu items from a file. But I have problem when I click an menu item i want it to mark that one as checked but I cant access the menu object of that item to see...
8
by: Huma123 | last post by:
Hi! My problem is that i cant able to run a simple c program cause of this error... Unable to open include file 'stdio.h' unable to open include file 'conio.h' while these header files r...
3
by: charmeda103 | last post by:
i am writng a program, what is my error i cant seem to figure out it. i got and error when an if-else statement in my code here is the if-else statement what the error, i cant figure it out. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.