C++ :: Iterative Loop Repeats More Times Than Expected

Nov 28, 2014

I tried my best but I can't figure out the problem. At the last part of "createArray", I output the final array created. I mean it to repeat once but then it repeat more times than I expect. createArray is an iterative function. If it repeats 3 times, than at last the array created which fulfil the criterion would be printed out 3+1 times.

I am trying to create an array with 3 numbers 5 times, resulting in a 2D array. The 3 numbers in a array are picked from 0 - 5. I enter createArray(5,3,5). Then these 5 arrays are compared with each other to see if there are repetitions. If there are, the whole process begins again, 5 arrays with 3 numbers each will be picked again and compared with each other. If there are no repetitions at last, there 5 arrays would be printed out.

void deleteArray(int** array){
delete[] array;
}
int** createArray(int simu_times, int randomrun,int numberofrun){
vector<Int_t>fChosenRun;
int** Array = new int*[simu_times];

[Code] ....

View 8 Replies


ADVERTISEMENT

C++ :: Do While Loop - Expected Unqualified ID

Jan 21, 2014

This first Dowhile loop is saying "expected unqualified-id" at the do and while parts. What does that mean

do {
do {
do {
std::cout << "Give me a test result or grade from 0 to 100 for class"<< 1 << std::endl;
std::cin >> mathaverage[mathtestcount];
loopresult=1;

[Code] .....

View 2 Replies View Related

C/C++ :: Expected Declaration Specifier In For Loop?

Mar 1, 2015

#include <stdio.h>
#include <math.h>
main() {
int n;
int k;
int j;
//gets user input for length of string
printf("Enter the value of n:");
scanf("%d", &n);

[code]......

When I compile my code I get an error saying that I need declaration specifiers in my is_prime subroutine in the for loop.

Here is the line of code it references.

for(j = 2; j < k; j++)

View 2 Replies View Related

C++ :: Loop A Set Number Of Times

Oct 29, 2013

I have been trying to make a program that asks for username and password, and if it is wrong it will loop back to the top. But i only want it to loop a set number of times, how can i do this? This is my code so far

string username;
string password;
cout << "Enter username: ";
getline(cin, username, '

[Code] ....

View 4 Replies View Related

C++ :: Create A Loop That Repeat Itself Many Times A Second?

Aug 6, 2013

I create a loop that would repeat itself many times a second? Trying to do extremely basic graphics with a grid and system("cls") every time I run a command, but it still looks very jumpy. I realize system calls are evil. Any way to get rid of this too.

View 1 Replies View Related

C++ :: Variables Declared - How Many Times Loop Executes

Jan 27, 2015

Assume all variable are declared correctly, the following for loop executes how many times?

for (i = 0; i <= 20; i++)
cout << I;

I think 20 but am not sure.

View 7 Replies View Related

C :: When Try To Iterate Two Times Or More - Program Gets Stuck In Infinite Loop

Mar 18, 2014

I'm having trouble getting my loop to work correctly. If I iterate the for loop once it works as expected and displays proper output. When I try to iterate two times or more the program gets stuck in an infinite loop.

testData8.dat:

Code:
12 9
13 756

View 3 Replies View Related

C/C++ :: For Loop To Print Given Character Number Of Times Specified By Integer

Feb 11, 2014

The function uses a "for" loop to print the given character the number of times specified by the integer.

How can I make a for loop to do that?

So.. my code looks like this:

// cpp : Defines the entry point for the console application
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void printMyInteger(int myInteger, char myChar) {

[Code] ....

So.. here is my error:

Error1error C2143: syntax error : missing ';' before ')'d:workspaceuniversity ools for games and animationworkshopsweek 6week 6week 6week 6.cpp101Week 6
Error2error C2143: syntax error : missing ';' before ')'d:workspaceuniversity ools for games and animationworkshopsweek 6week 6week 6week 6.cpp101Week 6
3IntelliSense: expected an expressiond:workspaceuniversity ools for games and animationworkshopsweek 6week 6week 6week 6.cpp107Week 6

View 3 Replies View Related

C++ :: Random Pick Without Repeats

May 9, 2014

What I'm trying to do is to have the program pick random people for different jobs. The user enters the number of people, then, enters both jobs and the people's names. I need the program to pick random people, but randomly pick again if the person has already been picked, so, no repeats. I have the program picking random people, but I can't get it to not pick the person again. I'll run it and there will be no errors, but it's almost like Xcode completely bi-passes the for/if to check if it's picked the person already.

Below is the code.

#include <iostream>
#include <math.h>
using namespace std;
int main() {
int runagain=0, correctnames=0, versiondesciosion=0, correctjobs=0;//vars that make descisions for running again
rerun:

[Code] ....

Dont look at the "fun" version for the switch statement. It's not ready yet, I'm just having trouble with the random thing.

View 3 Replies View Related

C++ :: For Loop - Code Puts Out Switch Statement 5 Times With Random Number

Jan 26, 2014

I am writing for loop with a switch so that scores can be inputted in by a judge. The issue that I am running into is that I will put out an the text then the test happens and the code puts out the switch statement 5 times with random number. Here is what I have written.

Code:
int main() {
int diver;
int option;
int Judge;
cout << "Enter Divers Name:";

[Code] ....

View 4 Replies View Related

C++ :: How To Turn Recursive Into Iterative

Jul 10, 2014

map< int, int > cache;
int count( int n ){
if( cache[n] != 0 ){
return cache[n];

[Code] ....

I don't know how to turn this recursive function into an iterative...

View 7 Replies View Related

C++ ::  From Recursive To Iterative Function

Jan 5, 2015

I am trying to make from f_rec (recursive function) to f_iter (iterative function) but I can't.

(My logic was to create a loop to calculate the results of f_rec(n-1), another loop for 2*f_rec(n-2) and one loop for f_rec(n-3);

But I'm wrong)

int f_rec(int n) {
if(n>=3)
return f_rec(n-1)+2*f_rec(n-2)+f_rec(n-3);

[Code] .....

I also think that my run time for the f_rec is 3^n ...

View 2 Replies View Related

C++ :: Turning Recursive To Iterative Form?

Jul 21, 2013

So I have a code like this one below :

void get_sum( INNER_ID id, vector<INNER_ID>& dont_check ) {
vector<INNER_ID> below = get_below( id );
vector<INNER_ID>::iterator second;

[Code]....

I use this algorithm for my "crappy" physic engine, so the point of this algorithm is to get the sum of mass below an object. get_below( id ) function can get the ids of what object is below them.

But before I need ids of the object below them to apply impulse, force, and some other physic stuff.

One object doesn't neccesarrly rest on top of one object, it can rest on 2 object or more.

when I look at it, it resemble a tree, maybe it's not. I just don't really know very much about tree algorithm

I cannot optimize a recursive code so I think, I better turn this into an iterative but I cannot seem to find a way to do that

View 4 Replies View Related

C++ :: BST Iterative Insert For Binary Search Tree

May 13, 2014

I'm writing the function as described in the title but it isn't quite working. It works as long as the value passed is less than the parent (going left) but when the value should be placed to the right, it doesn't actually insert the node.

template <typename T>
void BST<T>::insertHelper(BST<T>::BinNodePtr &subRoot, const T& item) {
BinNode *newNode;
BinNode *parent;
BinNode *child;

[Code] ....

FYI, I've commented out setting the children of the new leaf to NULL because the constructor already does that.

View 1 Replies View Related

C/C++ :: How To Programmatically Find If The Code Is Recursive Or Iterative

Apr 8, 2014

Is there any way to programatically find if the given code is taking recursive approach or iterative apporaoch using concept of files in C programming.

View 3 Replies View Related

C++ :: Binary Search Tree (Iterative Function To Insert)

Jun 23, 2013

I was studying BST and tried to make a iterative function to insert, the original recursive function is the following:

void insert(node *&tree, int value) {
if (!tree) {
tree = new node;
tree->num = value;
tree->left = tree->right = NULL;

[Code] ....

And the code that i did is (but doesn't work):

void insert(node *&tree, int value) {
if (!tree) {
tree = new node;
tree->num = value;
tree->left = tree->right = NULL;

[Code] ....

I don't see where the error is or why it doesn't work.

View 6 Replies View Related

C/C++ :: Expected Initializer Before Int

Jan 21, 2015

#include <iostream>
int ival1
int ival2=1
int summe
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {

[Code] .....

If I compile it I've got these errors

[Error] expected initializer before 'int'

recipe for target 'rechnen.o' failed

View 14 Replies View Related

C++ :: ATM - Expected Primary Expression Before Else

Mar 29, 2014

I keep getting an error here and cant quite figure out why,

Code:
else if (mainMenu == 3){
cout << "Please make a selection" << endl
<< " 1) Withdraw from account" << endl
<< " 3) Back to main menu" << endl;
cin >> withdrawMenu;

if (withdrawMenu == 1){

[Code] ....

View 1 Replies View Related

C :: Expected Expression Before Token

Jun 18, 2014

I wrote a program, that generates 20 random integers, and stores them in an array. Then I decided, to build more functions to it, i.e why not have it to display the lowest integer of an array. I created my function,

Code:
int minValue( int field[ ] )

and got my code in side, which (technically) works. In my main() function I'm calling

Code:
printf( "The smallest value of an array is: %d
", minValue( field[] ) );

and I'm getting an error trying to compile it.

Code:
randomArray.c:62:74: error: expected expression before ']' token
printf( "The smallest value of an array is: %d
", minValue( field[] ) );

View 2 Replies View Related

C++ :: Expected Primary Expression Before INT

May 26, 2014

This is what i have example code in c++:

#include <iostream>
class Foo{
public:
void bar(){
std::cout << "Hello" << std::endl;

[Code] ....

After compiling it is giving error as :
foo.cpp: In function ‘int Foo_max(Foo*)’:
foo.cpp:26:37: error: expected primary-expression before ‘int’
foo.cpp:26:46: error: expected primary-expression before ‘int’

View 6 Replies View Related

C++ :: Error - Expected Unqualified ID

Dec 2, 2014

I can't figure out this error.

#include <iostream>
using namespace std;
bool isPrime(int number); {
primeNumber = isPrime(number);

[Code] ....

View 2 Replies View Related

C++ :: Name Resolution Not Working As Expected

Apr 23, 2013

I have a global var (m) with an initial value 5.

I have a template class (A) that derives from a either a base class that has a member (_A1.m) or not (_A0), based upon it's template parameter. class (A) has a member function (fn) returns the value of (m) as it understands what (m) is.

However, this gives different results compared with a non-template class in a similar scenario. I'm expecting that if derived from _A1, that m should be taken from the base class scope and if derived from _A0, it should be taken from the global one.

Here is the code for your amusement:

int m = 5;
class _A0 {
public:
_A0(int) {

[Code] ....

This compiled using g++ 4.5.3 and 4.6.3 with the same results:
Global value of m is: 5
B0 class has no internal m member. Object resolves m internally with value 5
B1 class has internal m member. Object resolves m internally with value 3
A<_A0> class has no internal m member. Object resolves m internally with value 5
A<_A1> class has internal m member. Object resolves m internally with value 5

View 2 Replies View Related

C++ :: Destructors Being Called More Than Expected?

May 20, 2013

In this code:

#pragma once
#include <iostream>
class CBox // Derived class {
public:
// Constructor
explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0) : m_Length(lv), m_Width(wv), m_Height(hv){}

[Code] .....

Before the program ends, at return 0;

I would expect the CBox destructor to be called 3 times but it is being called 6 times? Why? Also in this code:

#pragma once
#include <iostream>
class CBox // Derived class {
public:
// Constructor
explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0) : m_Length(lv), m_Width(wv), m_Height(hv){}

[Code] .....

Why is the destructor called 3 times? When have you really destroyed a CBox? Doesnt emplace only create it and store it, then thats it?

[URL] .....

For pushback:

void push_back(value_type&& _Val)
{// insert by moving into element at end
if (_Inside(_STD addressof(_Val)))
{// push back an element
size_type _Idx = _STD addressof(_Val) - this->_Myfirst;

[Code] .....

View 12 Replies View Related

C :: Float Variable Increases More Than Expected

Sep 14, 2014

I just wrote the following simple program:

Code:
#include <stdio.h>
#include <math.h>
int main(void) {
float fx, x;

[Code] ....

The output increases x by 0.1 as expected until x=1.5. After that x becomes 1.600001 and not 1.6 as expected.

View 9 Replies View Related

C++ ::  Expected Identifier With Numeric Limits

Aug 22, 2014

I'm trying to perform a simple input operation, and check to make sure that whatever the user input is a valid input (is within data type bounds, is correct data type...). But when I get to ignoring the input, I keep getting an error.

unsigned short num;
while (true) {
std::cin >> num;
if (std::cin.fail()) {
num = 1;

[Code] ....

I don't fully understand why this error is here.

View 6 Replies View Related

C/C++ :: Error / Expected Initializer Before String

Nov 15, 2014

I'm working on a school project learning C++. I'm running into an error I can't seem to find the answer for (see topic title).

main.cpp
#include <iostream>
#include <string>
#include "Signature.h"
#include "Genome.h"
using namespace std;
// Calling Function
int main() {
Signature Sig1; // Create Signature object
Genome gString1; // Create Genome object

[code]....

View 1 Replies View Related







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