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 ;) -
#include <iostream>
-
using namespace std;
-
#include <iomanip>
-
-
-
-
-
int find_maximum(int a[], int n)
-
{
-
int maximum=a[0];
-
for (int i=0;i<n;i++)
-
{
-
if(a[i]>maximum)
-
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<n; i++)
-
{
-
sum = sum + a[i]; // Add the next element to the total
-
}
-
-
cout << sum << endl;
-
cout<<"The maximum value for this array is "<<find_maximum(a,n)<<endl;
-
cout<<"Enter the value you wish to find it's position in the array";
-
cin>>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
5 1629 -
#include <iostream>
-
using namespace std;
-
#include <iomanip>
-
-
-
-
-
int find_maximum(int a[], int n)
-
{
-
int maximum=a[0];
-
for (int i=0;i<n;i++)
-
{
-
if(a[i]>maximum)
-
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<n; i++)
-
{
-
sum = sum + a[i]; // Add the next element to the total
-
}
-
-
cout << sum << endl;
-
cout<<"The maximum value for this array is "<<find_maximum(a,n)<<endl;
-
cout<<"Enter the value you wish to find it's position in the array\n";
-
cin>>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
your input loop -
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
your input loop -
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 :)
[if the loop exits on an error condition you need to clear the error and remove the faulty characters, e.g. -
while (cin >> a[n])
-
{
-
n++;
-
}
-
cin.clear(); // clear error condition
-
cin.ignore(1000,'\n'); // discard any characters to newline
-
[if the loop exits on an error condition you need to clear the error and remove the faulty characters, e.g. -
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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(...
|
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...
|
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.
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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.
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |