C++ :: Separating Numbers Using Reference

Oct 25, 2013

Write a function called breakThree that will accept a 3 digit integer and returns each of the numbers individually. This function will take four paramaters. The first parameter is the three digit number to break apart. Parameters 2 through 4 are passed by reference and will be used to return each of the numbers back to main.

You should make sure that the input into the function is a 3-digit number. If it is not a three digit number the breakThree function should simply return false. If it is a three digit number the breakThree function should break the number apart, and store each of the numbers in the parameters passed by reference.

In main you should get the number from input and then output each of the numbers on a separate line.

What not to use
global variables
cin in breakThree function
cout in breakThree function
goto statements

#include <iostream>
using namespace std;
void separate(int a, int b, int c, int d);
int main(int argc, const char * argv[]) {
int num;

[Code] ....

View 4 Replies


ADVERTISEMENT

C++ :: Separating Prime Numbers In Group

May 25, 2013

I currently need separating my Prime numbers in group for my assignment. I absolutely do not know how to do this. I am able to find out if the number I used is prime and see how many prime number there are between 1-100 and then 1-1000 (168 primes) and so on. The thing that I need to do is find the number of prime numbers between 101-200, 201-300, 301-400, and so on until 901-100 and have it show on screen. Here is the exact assignment that I’m supposed to find out:

Assignment:

Write a C++ program to calculate and display the number of primes from 1 to 100,000, separated in groups of 100, 10 groups per line. To be more precise, on the first line of output, display the number of primes between 1 and 100, 101 and 200, etc., up to 901 to 1000. Then display the average primes per group. For example, there are 168 primes from 1 to 1000, so the average number of primes per group of 100 (or percentage) is 16.8. Then repeat the process for the groups of 100 between 1001 to 1100, 1101 to 1200, etc., for all groups of 1000 all the way up to 100,000. Your results should be as presented below, under testing.

Testing:

The output of your program should be the following, according to my calculations. Each group (g1, g2, etc.) is the number of primes in a group of 100 numbers. For example in line 1, g1 is the group from 1 to 100, g2 is the group 101 to 200, etc. For line 2, g1 is the group 1001 to 1100, g2 is 1101 to 1200, and g10 is the group from 1901 to 2000.

g1 g2 g3 g4 g5 g6 g7 g8 g9 g10 Average Primes
----------------------------------------------------------------
25 21 16 16 17 14 16 14 15 14 16.8 for 1 to 1000
16 12 15 11 17 12 15 12 12 13 13.5 for 1001 to 2000
14 10 15 15 10 11 15 14 12 11 12.7 for 2001 to 3000
12 10 11 15 11 14 13 12 11 11 12.0 for 3001 to 4000



7 10 5 10 7 11 7 6 11 8 8.2 for 97001 to 98000
7 5 9 9 11 8 7 8 12 11 8.7 for 98001 to 99000
8 11 8 8 7 9 8 10 10 8 8.7 for 99001 to 100000

Total number of primes from 1 to 100000: 9592
Average primes per 1000: 95.92
Percentage of primes from 1 to 100000: 9.59

Here is my code that I have done so far:

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
bool isPrime(long candidate);
long primeCount (long start, long end);

[Code] ....

View 8 Replies View Related

C :: Separating Number Num-by-num

Nov 7, 2014

I'm trying to separate number num-by-num but according to the example it must show 501 but it shows 510.

Code:

#include <stdio.h>
#include <math.h>
int power(int base,int expo)
{
if ( expo!=1 )
return (base*power(base,expo-1));
}

[code]....

View 1 Replies View Related

C++ :: Separating Abstraction And Implementation

May 13, 2012

What is the benefit on separating the abstraction and implementation.

The below code

Code:
class FileProcessor {
public:
virtual void processFile();
};
class DocProcessor : public FileProcessor

[Code] ....

Till now it is fine. suppose DocProcessor and ExcelProcessor uses two big algorithm to process these file types then I still can have subclasses like :

class Algorithm1 : public DocProcessor
{
public:
void algorithm(); //which will be called as per the algorithm class instantiated for to process
};

[Code] ....

same for ExcelProcessor class

Here everything looks fine to me. [ I have not used the bridge pattern to separate the abstraction and implementation]. The only thing i can achieve using bridge is that the number of classes will be reduced if new class like jpgProcessor is introduced or new algorithim is introduced.

Then why it is recommended that always separate the abstraction and implementation...

View 5 Replies View Related

C++ :: Storing Numbers In Array - Pointer And Reference

Apr 17, 2012

I just started learning about pointer and reference. * and &

The assignment is " Write a program that stores the following numbers in the array named miles:15,22,16,18,27,23, and 20. Have your program copy the data stored in miles to another array named dist, and then display the values in the dist array. YOur program should use pointer notation when copying and displaying array elements.

And this is what i have so far. But there is an error. I highlighted it with red. It says it's incompatible...

#include <iostream>
using namespace std;
const int arraynumb = 7; // declaration of keys: number of characters of keys
void copyfunc(int *[], int); // function initialized
int main() {
int miles[arraynumb] = {15, 22, 16, 18, 27, 23, 20};

[Code] ....

View 1 Replies View Related

C++ :: Random Numbers (Representing X And Y Co-ordinate) Grid Reference

Sep 18, 2013

How to code the following:

I have two random numbers which are generated and are related to each other (representing x and y co-ordinate). These random numbers are generated within a specified range: (-range, +range).

I want to categorize these values in a (2-dimensional) grid. The grid size is not definite and so can be varied by the user would be in the order of 400 x 400. (e.g., think CCD detector). For each random number pair (x, y) I want to store a hit (a plus one) in the corresponding grid reference.

In the order of 500,000 related random numbers (x and y) are to be generated and the position recorded according to grid reference. So code needs to be fast.

View 8 Replies View Related

C :: Reading A File In And Separating Line With Strtok Function

May 2, 2013

I'm trying to read a file from the 2nd argument, skipping the first line of the file since it's the name of the columns, and separate each line as a token, by date, subject, startTime, endTime, and location.Here's my code that I got so far:

Code:

void readAppointment(){
if (arg != 2)
exit;
else {
FILE *fp;
fp = fopen( argv[1], "r");
char line[999];

[code]....

View 1 Replies View Related

C++ :: Separating Routines Into A Separate Implementation And Header File

Oct 18, 2013

I am trying to separate out particular sets of routines into a separate implentation and header file which can be compiled independently to the main program such that the program source consists of a file called customers.h, customers.cpp, and exercise_1_5.cpp

Each of the files should contain the following:

customers.h should contain the definition of the customer structure and the declaration of print_customers.

customers.cpp should contain the implementation (or definition) for print_customers.

exercise_1_5.cpp should contain an include of customers.h and the main program.

This is my original code from a single .cpp file

#include<iostream>
#include<string>
using namespace std;

[Code].....

The error messages I am getting from the compiler on the customers.cpp file:

C:UsersBenDocumentsCS264lab3customers.cpp:5:22: error: variable or field 'print_customers' declared void
C:UsersBenDocumentsCS264lab3customers.cpp:5:22: error: 'customer' was not declared in this scope
C:UsersBenDocumentsCS264lab3customers.cpp:5:32: error: 'head' was not declared in this scope

View 6 Replies View Related

C++ :: Looping - Input Numbers Until User Types 0 Then Output Product Of Non Zero Numbers

May 1, 2014

How to do the problem below using loop?

Input numbers until the user types a 0, then output the product of the non 0 numbers: e

E.g., if the user types 2 4 6 0, the program should output 48

View 3 Replies View Related

C :: Finding Written Numbers In A String And Converting Them To Decimal Numbers

Jun 20, 2013

User enters sentence "The Smiths have two daughters, three sons, two cats and one dog." (The numbers may change depending on what the user chooses to enter. He told us the range would be from zero to nine.) and we have to convert the written numbers within the sentence into actual decimal numbers and print out the new sentence. Ex. The Smiths have 2 daughters, 3 sons...etc.

I have written the following bit of code which reads the string and finds all the "written numbers" but I am not sure how to proceed from there. I am stuck on how to print out the new sentence with the converted numbers as my professor mentioned something about creating the new string using dynamic memory allocation.

Code:
#include <stdio.h>#include <string.h>
int main () {
char A[100];
int length = 0;
int i;

[Code] .....

View 7 Replies View Related

C++ :: Find Prime Numbers Between Given Pair Of Numbers And Store Them Into Array?

Apr 18, 2014

Find all the prime numbers between a given pair of numbers. Numbers should be read in from an input file called "numbers.txt" and find all the prime numbers between them. Store the prime numbers in an array, then sort the array from greatest to least. Display the array before and after the sort.

I'm stuck on how to put the prime numbers into an array.

The input file has the numbers 1 & 100.

Here's what I have so far.

#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream fin;
fin.open("numbers.txt");

[Code] .....

View 1 Replies View Related

C :: Find Duplicate Numbers And Numbers Found Once In Array

Dec 7, 2013

Question: How to find a duplicate numbers and numbers found once in array.

View 7 Replies View Related

C++ :: Numbers Class - Translate Whole Numbers To English Description

Feb 4, 2015

I'm working on this program that I have to design a class Numbers that can be used to translate whole numbers to the English description of the number.

Now this is what I got so far:

#include <iostream>
#include <string>
using namespace std;
class Numbers {
private:
int number;
static string ones[];
static string tens[];

[Code] ....

The program seems to work. However its not giving me the right number description,

Example:

Please enter the amount you would like translated into words: 5
six dollars
please enter another number: 10
eleven dollars
please enter another number: 20
thirty dollars
please enter another number: 30
forty dollars
please enter another number: 100
two hundred dollars
please enter another number: 150
two hundred sixty dollars
please enter another number: 500
six hundred dollars
please enter another number: 1000
two thousand dollars
please enter another number:

View 4 Replies View Related

Visual C++ :: Ignoring Negative Numbers When Trying To Add Only Positive Numbers?

May 15, 2013

ignoring negative numbers when I am trying to add up only positive numbers.

SAMPLE:
if (num>=0) {
sum= sum + num;
}
else

how would the else in this case being a negative number not be included in the sum

View 4 Replies View Related

C++ :: How To Reference An Array

Jan 12, 2013

Im trying to reference my array in another function but i keep getting errors.

void player::store()
{
int menuChoice;
int amountChoice = 0;
int items[4] = {0,0,0,0};
string inv;

[Code]...

errors

C:UsersChayDesktopDinosaur ArenaMainGame.h|81|error: declaration of 'items' as array of references|
C:UsersChayDesktopDinosaur ArenaMainGame.h|81|error: prototype for 'void player::backpack(...)' does not match any in class 'player'|
C:UsersChayDesktopDinosaur Arenaplayer.h|24|error: candidate is: void player::backpack()|

View 4 Replies View Related

C++ :: When Does A Reference Become Invalid

Mar 20, 2013

I tried to answer the question myself and came up with an example.

#include<iostream>
using namespace std;
class A {
public:
int a;
A(int aa) : a(aa) { }
~A() { cout<<"~A()

[code]....

why statements (*) and (**) work ? Since the object a gets destroyed, shouldn't rA be invalid ? Or this is just undefined behavior ?

View 2 Replies View Related

C++ :: Are Reference And Address Same

Aug 2, 2014

Are Reference and Address same or Different?

View 10 Replies View Related

C++ :: Pass By Value And Reference

Nov 25, 2013

(Pass-by-Value vs. Pass-by-Reference)

Write a complete C++ program with the two alternate functions specified below, each of which simply triples the variable count defined in main. Then compare and contrast the two approaches. These two functions are

a) function tripleByValue that passes a copy of count by value, triples the copy and returns the new value and

b) function tripleByReference that passes count by reference via a reference parameter and triples the original value of count through its alias (i.e., the reference parameter).

View 7 Replies View Related

C :: Subset Combinations - Select All Numbers Right Or No Numbers Right

Dec 22, 2013

I need a list generated of all possible subset combinations for the set 1,2,3,4,5,6,7,8,9,10,12. Select six. Numbers cannot repeat.

Example subset: 1,2,3,4,5,6 (six selected, no repeats).

Example of what I dont need: 1,1,2,2,3,3,4,4,5,5,12,12 or 1,1,1,1,1,6.

I will also need the opposites removed, meaning...if I have 1,3,5,7,9,11 then I need 2,4,6,8,10,12 eliminated from the final list.

This is for a game, where you must select all numbers right or no numbers right.

View 2 Replies View Related

C/C++ :: Generate Combinations Of Numbers From 1 To 25 In 15 Numbers Array?

Sep 21, 2014

The code below will generate combinations of numbers from 1 to 25 in an 15 numbers array. The only filter I've applied is that the sum of all the numbers in the vectors divided by 15 needs to be between 13 and 14.
I would like to count how many consecutive numbers there are in one combination, so that later i can apply another filter.. for example:

1 3 4 5 6 8 10 13 14 16 17 18 19 20 25

3 + 4 = 1
4 + 5 = 1
5 + 6 = 1
13 + 14 = 1
16 + 17 = 1
17 + 18 = 1
18 + 19 = 1
19 + 20 = 1
_____________

Count = 8, in this case..

I think it's not very difficult to do, but i just can't see how to do it.

#include <iostream>
#include <vector>
#include <numeric>

[Code]....

View 3 Replies View Related

C/C++ :: Program To Print Product Of Even Numbers And Sum Of Odd Numbers Between 1 And 30

May 20, 2014

I want to make a program to print the product of even numbers between 1 and 30 and sum of odd numbers between 1 and 30. But the answer of product is negative. The photo shows the output of the code.

#include <stdio.h>
#include <conio.h>
void main ()
{
int i, even_product=1, odd_sum=0;
for(i=1;i<=30;i++) // For loop starts here!

[Code]...

View 5 Replies View Related

C++ :: Pass Array By Reference

Apr 10, 2014

I need to pass an array of 10 instances of a custom class to a function. The snippets of code are posted below. How would I do this right?

The prototype:

Code:
int Search(Vertex vertex[], ofstream &outfile);

The implementation in the main function.

Code:
Search(vertex[10], outfile);

View 2 Replies View Related

C++ :: Returning Reference Of Vector

Sep 6, 2013

Example code:

Code:
#include <iostream>
#include <vector>
using namespace std;

class A{

[Code]....

I read somewhere, that we can imagine the reference as a pointer to the vector. So, my question is:

Let's assume that instance of class A, named a, was created with new. We call a.getV() to foo and then we call the destructor of a. foo is safe? Is the copy constructor of std::vector called?

View 1 Replies View Related

C++ :: Passing A Reference Of Arg (boost Lib)

Dec 19, 2013

I have in my main(), a function that creates my arg object using boost/program_options.hpp i want to pass this object to another function using templates like this:

Code:
template <typename Targ>
void Count(Targ & arg){
MyObj<string> QueryTab(arg["input-file"].as<string>()); //line number is 352
...
}

However I get an error:

Code:
../include/Filter.hpp: In member function ‘void Count(Targ&)’:
../include/Filter.hpp:352:40: error: expected primary-expression before ‘>’ token
../include/Filter.hpp:352:42: error: expected primary-expression before ‘)’ token
... obviously it does not recognize my intention, what did I do wrong?

View 2 Replies View Related

C++ :: Undefined Reference To Push (int)

Apr 20, 2013

The problem with the code is on line 14 and says undefined reference to `push(int)

Code:
#include<iostream>
using namespace std;
void push(int n);
int pop(int &n);
struct elem{
int key;

[Code]...

View 2 Replies View Related

C :: Multiple Reference In One Pointer

Aug 20, 2014

Can I a have one pointer with two reference in it. Here's what I've got.

Code:
char* c;
char x='x' , y='y';
c = &x;
c = &y; -- or --
Code: char* c[2];
char x='x' , y='y';
c[0] = &x;
c[1] = &y;

If it's possible I want to apply it to make AST.

View 8 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved