There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
.dbx file audio avg avg 8 brand new bsod chinese codec conversion crash desktop display dos driver duplicate dvd error error message excel explorer file firefox game hardware hijackthis log hjt install installation internet ipod javascript laptop low disk space macro malware msconfig msn music nero express network outlook outlook 2003 outlook express php pro problem rows rundll32 security seo sound sp3 spyware switch tag cloud trojan usb video virus vista vundo wallpaper windows windows vista windows xp wireless word xp service pack xp sp3 youtube
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
C++ Help


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
bootsiebella's Avatar
Junior Member with 12 posts.
 
Join Date: Mar 2008
09-May-2008, 10:34 AM #1
C++ Help
Can anyone help me write these algorithm in C++

Write an algorithm to read a sequence of integers, ignoring all numbers that are less than 1. Count how many even and odd numbers there are, and print these counts. For example, with input 7 25 100 2 -6 1 0 3, the output should be
Even = 2, odd = 4
There were two even numbers (100 and 2), four odd numbers (7, 25, 1, and 3), and two numbers were ignored (-6 and 0). Your algorithm should work for any input, not just this example.

4. A tollbooth needs a program that tells how much to charge drivers. The charge is based on how many people are in the vehicle. To encourage car-pooling, the cost is lower for more people. The cost is $6.00 for one person, 1/2 of that ($3.00) for two people, 1/3 ($2.00) for three people, and in general 6/n for n people.

Write an algorithm that reads the number of people in the vehicle and prints the cost. Print "ERROR" if a number less than 1 is entered. It continues reading input in a loop.
Shadow2531's Avatar
Distinguished Member with 2,621 posts.
 
Join Date: Apr 2001
11-May-2008, 08:13 AM #2
For the first one, how far have you gotten? Have you figured out how to check if an integer is odd or not?

Are you allowed to use std:string, std::vector and std::istringstream?
TheRobatron's Avatar
Computer Specs
Senior Member with 298 posts.
 
Join Date: Oct 2007
Location: Farnborough, England
Experience: Intermediate
14-May-2008, 05:41 PM #3
For the first one you'll need a function to check if the number is even:

bool even(int num) {
if (num % 2 == 0) {
return true;
} else {
return false;
}
}

Then you can keep totals of each:

int totalEvens, totalOdds;
if (even(num)) {
totalEvens++;
} else {
totalOdds++;
}

You can put it all in a for or while loop and to check if the number is valid all you need is a simple if statement to check if the input is more than 0.
__________________
There's no place like 127.0.0.1

Reply


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:19 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.