|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Removing sentinel from data (C++)
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||
|
AMD Overclocker
|
Hey guys,
__________________I'm almost done with my code. One issue that's bugging the bejeezus out of me is how to "not count" the sentinel as part of the data. For instance, in the program below, I have the user continuously enter in values until sentinel (aka "11") is entered. Once that happens, the data is added up, the lowest and highest numbers found. However, unless the user were to specifically type in numbers BOTH BIGGER AND SMALLER than the sentinel, the sentinel will be either the max or min! ![]() ![]() How can I remove the sentinel from being counted?Code:
#include <iostream>
using namespace std;
int main(){
int value[] = {0};
int sentinel = 11;
int sum = 0;
int x;
int minimum = 1000;
int maximum = 0;
for(int i = 0; value[i] < x; i++)
{
while(value[i] != sentinel)
{
maximum != sentinel;
cout << "Please enter a number: ";
cin >> value[i];
sum += value[i];
if(value[i] < minimum)
minimum = value[i];
if(value[i] > maximum)
maximum = value[i];
if(value[i] < 1 || value[i] > 999)
{
cout << "Invalid number. Please try again!" << endl;
continue;
}
}
if(value[i] = sentinel)
cout << endl << "Sentinel reached! " << endl;
break;
}
cout << endl << "The sum is " << sum - sentinel << endl;
cout << endl << "The minimum is " << minimum << endl;
cout << endl << "The maximum is " << maximum << endl;
getchar();
getchar();
return 0;
}
|
|||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
Programmer
![]() |
Basically you need to break from the loop when the sentinel is reached. Your current code still continues the loop since it doesn't check for the sentinel until the beginning of each iteration.
change your "while(value[i] != sentinel)" to "while(1)" and right after the "cin>>value[i]" line add "if (value[i] == sentinel) break;" Note the red sections: Code:
#include <iostream>
using namespace std;
int main(){
int value[] = {0};
int sentinel = 11;
int sum = 0;
int x;
int minimum = 1000;
int maximum = 0;
for(int i = 0; value[i] < x; i++)
{
while(1)
{
maximum != sentinel;
cout << "Please enter a number: ";
cin >> value[i];
if (value[i] == sentinel)
break;
sum += value[i];
if(value[i] < minimum)
minimum = value[i];
if(value[i] > maximum)
maximum = value[i];
if(value[i] < 1 || value[i] > 999)
{
cout << "Invalid number. Please try again!" << endl;
continue;
}
}
if(value[i] = sentinel)
cout << endl << "Sentinel reached! " << endl;
break;
}
cout << endl << "The sum is " << sum - sentinel << endl;
cout << endl << "The minimum is " << minimum << endl;
cout << endl << "The maximum is " << maximum << endl;
getchar();
getchar();
return 0;
}
The "while(1)" creates an infinite loop since 1 is always 1 (could be rewritten as (true) or (1==1)). The "if statement" checks for the sentinel, and if it is there, it breaks from the loop before anything is done with it.
__________________
Last edited by xtascox : 10-17-09 at 01:52 AM |
|||||||||||||
|
|
|
|
|
#3 (permalink) | |||||||||
|
AMD Overclocker
|
Thanks a bunch! Got it to work PERFECT. +1 rep
__________________Also, while(1), shouldn't every loop that is trying to use a sentinel-type function be using that rather than say while(x != y) or while(x < > y)? Like, always always? EDIT: Is my formatting any better? I've gotten complaints from members due to bad formatting...I've been practicing spacing things out and what not, I assume this is OK?
Last edited by godsgift2dagame : 10-17-09 at 02:01 AM |
|||||||||
|
|
|
|
|
#4 (permalink) | |||||||||||
|
4.0ghz
![]() |
A better way to do it would be like this (although you have to repeat a small section):
Code:
maximum != sentinel;
cout << "Please enter a number: ";
cin >> value[i];
while(value[i] != sentinel)
{
sum += value[i];
if(value[i] < minimum)
minimum = value[i];
if(value[i] > maximum)
maximum = value[i];
if(value[i] < 1 || value[i] > 999)
{
cout << "Invalid number. Please try again!" << endl;
continue;
}
maximum != sentinel;
cout << "Please enter a number: ";
cin >> value[i];
}
__________________
When asking for help: state the goal, not the step.
|
|||||||||||
|
|
|
|
#5 (permalink) | ||||||||||||||
|
Programmer
![]() |
Quote:
![]() With the loop, it really depends on how you right it. In the case of the way you wrote your code, it was easier to use "while(1)" instead of rewriting it like Coma showed.
__________________
|
||||||||||||||
|
|
|
|
|
#6 (permalink) | |||||||||
|
AMD Overclocker
|
Here's the official COMPLETE AND FULLY FUNCTIONING program. Thanks for the help guys!
__________________Code:
#include <iostream>
using namespace std;
int main(){
int value[] = {0};
int sentinel = 11;
int sum = 0;
int x;
int minimum = 1000;
int maximum = 0;
for(int i = 0; value[i] < x; i++)
{
while(value[i] != sentinel)
{
maximum != sentinel;
cout << "Please enter a number: ";
cin >> value[i];
sum += value[i];
if (value[i] == sentinel)
break;
if(value[i] < minimum)
minimum = value[i];
if(value[i] > maximum)
maximum = value[i];
if(value[i] < 1 || value[i] > 999)
{
cout << "Invalid number. Please try again!" << endl;
continue;
}
}
if(value[i] = sentinel)
cout << endl << "Sentinel reached! " << endl;
break;
}
cout << endl << "The sum is " << sum - sentinel << endl;
cout << endl << "The minimum is " << minimum << endl;
cout << endl << "The maximum is " << maximum << endl;
getchar();
getchar();
return 0;
}
|
|||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|