Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 10-17-09   #1 (permalink)
AMD Overclocker
 
amd nvidia

Join Date: Apr 2009
Posts: 429

Rep: 19 godsgift2dagame Unknown
Unique Rep: 18
Trader Rating: 0
Default Removing sentinel from data (C++)

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;
}
__________________
System: $775
CPU
Phenom II 720
Motherboard
Gigabyte MA790X-UD4P
Memory
4GB OCZ Reaper DDR2 1150
Hard Drive
250GB Seagate Barracuda 7200.10 x 2
Power Supply
PC Power & Cooling 610W
Case
Antec 300
CPU cooling
XIGMATEK Dark Knight-S1283V
OS
Vista 64-bit
godsgift2dagame is offline   Reply With Quote
Old 10-17-09   #2 (permalink)
Programmer
 
xtascox's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: Dallas, PA
Posts: 778

Rep: 37 xtascox is acknowledged by some
Unique Rep: 36
Trader Rating: 1
Default

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.
__________________

System: SVT
CPU
Q6600 @ 3.2ghz 1600fsb
Motherboard
EVGA 780i FTW
Memory
4GB OCZ Platimum
Graphics Card
2X EVGA 8800GTS 512
Hard Drive
160GB Seagate Barracuda 7200 SATA
Sound Card
Onboard Realtek ALC883
Power Supply
CORSAIR 750TX
Case
Chenming 901A
CPU cooling
Arctic Freezer 7 + Arctic Silver 5
GPU cooling
Stock Heatsink and fan
OS
Windows Vista Ultimate 64
Monitor
Hanns-G 22" LCD

Last edited by xtascox : 10-17-09 at 01:52 AM
xtascox is offline   Reply With Quote
Old 10-17-09   #3 (permalink)
AMD Overclocker
 
amd nvidia

Join Date: Apr 2009
Posts: 429

Rep: 19 godsgift2dagame Unknown
Unique Rep: 18
Trader Rating: 0
Default

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?
__________________
System: $775
CPU
Phenom II 720
Motherboard
Gigabyte MA790X-UD4P
Memory
4GB OCZ Reaper DDR2 1150
Hard Drive
250GB Seagate Barracuda 7200.10 x 2
Power Supply
PC Power & Cooling 610W
Case
Antec 300
CPU cooling
XIGMATEK Dark Knight-S1283V
OS
Vista 64-bit

Last edited by godsgift2dagame : 10-17-09 at 02:01 AM
godsgift2dagame is offline   Reply With Quote
Old 10-17-09   #4 (permalink)
4.0ghz
 
Coma's Avatar
 
intel nvidia

Join Date: Jun 2007
Posts: 7,471

Rep: 455 Coma is a proven memberComa is a proven memberComa is a proven memberComa is a proven memberComa is a proven member
Unique Rep: 328
Trader Rating: 0
Default

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.

System: Akiyama Mio
CPU
E6420 @ stock, 0.98v
Motherboard
Asus P5N-E SLI
Memory
2x1GB OCZ Platinum @ 800MHz 4-4-4-12 1T, 1.9v
Graphics Card
BFG 8800GT 512MB
Hard Drive
WD 250GB, 320GB SATA/3, 16MB Cache
Power Supply
Corsair 520HX
Case
NZXT Apollo Black
CPU cooling
Stock
OS
Ubuntu 9.04 x86 & XP x86
Monitor
Asus VW222U
Coma is offline Overclocked Account   Reply With Quote
Old 10-17-09   #5 (permalink)
Programmer
 
xtascox's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: Dallas, PA
Posts: 778

Rep: 37 xtascox is acknowledged by some
Unique Rep: 36
Trader Rating: 1
Default

Quote:
Originally Posted by godsgift2dagame View Post
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?
Formatting looks good to me.

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.
__________________

System: SVT
CPU
Q6600 @ 3.2ghz 1600fsb
Motherboard
EVGA 780i FTW
Memory
4GB OCZ Platimum
Graphics Card
2X EVGA 8800GTS 512
Hard Drive
160GB Seagate Barracuda 7200 SATA
Sound Card
Onboard Realtek ALC883
Power Supply
CORSAIR 750TX
Case
Chenming 901A
CPU cooling
Arctic Freezer 7 + Arctic Silver 5
GPU cooling
Stock Heatsink and fan
OS
Windows Vista Ultimate 64
Monitor
Hanns-G 22" LCD
xtascox is offline   Reply With Quote
Old 10-17-09   #6 (permalink)
AMD Overclocker
 
amd nvidia

Join Date: Apr 2009
Posts: 429

Rep: 19 godsgift2dagame Unknown
Unique Rep: 18
Trader Rating: 0
Default

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;
}
__________________
System: $775
CPU
Phenom II 720
Motherboard
Gigabyte MA790X-UD4P
Memory
4GB OCZ Reaper DDR2 1150
Hard Drive
250GB Seagate Barracuda 7200.10 x 2
Power Supply
PC Power & Cooling 610W
Case
Antec 300
CPU cooling
XIGMATEK Dark Knight-S1283V
OS
Vista 64-bit
godsgift2dagame is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 05:49 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.12961 seconds with 8 queries