C++ :: While Loop - Use Non-recursive Function To Make Triangle Number Sum Of All Whole Numbers From 1 To N

Jul 31, 2014

My while loop is the problem. I want to use a non-recursive function to make the triangle number the sum of all whole numbers from 1 to N.

#include "stdafx.h"
#include <iostream>
using namespace std;
int triangle(int t);

[Code] ....

View 2 Replies


ADVERTISEMENT

C/C++ :: Find Largest Sum Down A Triangle - Code Error With Recursive Function

Sep 29, 2013

Thing I want is find the largest sum down a triangle,(moving to adjacent numbers on the row below )there are many methods to go down.

75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23

I wrote a program with a recursive() called finder. But it dose not work properly,at run time it becomes to a infinite status. How to detect the error at runtime. Here is the code.

#include<stdio.h>
void finder(int x,int y);
int tot;

[Code] ....

I think the error is the changing value of x after a round of for loop.

View 2 Replies View Related

C :: Converting For Loop To Recursive Function

Jun 10, 2013

I am working on a problem that requires a nest for loop to be converted to a recursive function. I am going to provide just the code instead of the entire program.

Code:

for (R1=1; R1 <+3, R1++){ //for loop
printf (something);
}
// the recursive function
void loopR1 (int R1, int max){
if (R1 <= max){
printf (something);

[Code]...

when calling the recursive function in main i am using the following statement...

loop r1(1,3)

View 4 Replies View Related

C++ :: Convert A Recursive Function To A Loop?

Dec 13, 2014

I am trying to convert a recursive function to a loop. However, it is not working.

//recursive function
int function(int x){
if (x % 3 == 0){

[Code]....

View 1 Replies View Related

C :: Recursive Function - Add All Even Numbers From N To 2

May 5, 2014

I'm writing a program that starts at a given number n and adds all the way to 2:

n + (n-2) + (n-4) + (n-6) + .....

The following is my code, it compiles but after I enter an integer the program crashes.

Code:
#include<stdio.h>
#include<stdlib.h>
int sum_even(int n);
int sum_even(int n){
if(n==1){

[Code] ....

View 11 Replies View Related

C++ :: How To Make Loop That Reiterates For Certain Number Of Players

Dec 11, 2013

Something that does something like this

at this point run a loop for all players, in this loop use random to determine the face and depending on the face run a loop (for roll again etc) or give next user the chance.

And this loop is 2-12 people, all rolling die.
They draw 3 die out of a cup of 12.
Then they roll the 3 die, getting fish, hook, or boot on each die.

View 1 Replies View Related

C++ :: Recursive Function Which Find Partition Of A Number N

May 18, 2013

I am supposed to write a recursive function which find the partition of a number n ,, for example if n=3 , it should print 1 1 1 , 1 2 , 3

I wrote the program but i am getting the two partition 1 2 and 2 1 which are the same ,, how can i avoid that ?
this is the code :

void PrintPartition( int n , int A[] , int j ) {
if( n<=0 ) {
printArray( A, j );
return ;
} for( int i=1 ; i<=n ; i++ ) {
A[j]=i;
PrintPartition( n-i , A ,j+1 );
} }

the first call of the function is : PrintPartition( n , A , 0 ) ;

View 3 Replies View Related

C++ :: Highest Digit In A Number (recursive Function)

Dec 23, 2013

I have this example problem in my school coursebook. How this program works? It determines the highest digit in a number.

#include <iostream>
using namespace std;
int m(int n) {
int a,b;

[Code] ....

View 4 Replies View Related

C++ :: Program To Make A Table For Any Input Number - Do While Loop

Nov 27, 2014

Write a program to make a table for any input number and then continuesly ask to press y to print more table and if you press any key other than y then program must be terminate using while loop and do while loop. How to start or end with it.

View 2 Replies View Related

C++ :: Creating Recursive Function That Will Print Out A Number To A Power

Apr 9, 2013

I am trying to create a recursive function that i can call on in order to take a user inputed base and exponent and give final answer this is what i have but im completely lost after this i dont even know how to continue. What i have so far

#include <iostream>
using namespace std;
int Exp(int x,int y){
if(base <= 1 || exp == 0) return 1;
if(exp == 1) return base;
int main(){
int number, exp;

[Code] .....

After i set the base situations im not sure how to get the function to make the function take the base to the exponent recursively.

View 3 Replies View Related

C++ :: Recursive Function - Program Is Returning A Negative Number At The End

Nov 14, 2013

Why my program is returning a negative number at the end...attached is the program:

/*Write a recursive function recursiveMinimum that takes an integer array and the array size as arguments and returns the smallest element of the array. The function should stop processing and return when it receives an array of 1 element.*/

#include <stdio.h>
#include <time.h>
#include <iostream>
using namespace std;
float recursiveMinimum (int ARRAY[], int n);

[Code] .....

View 2 Replies View Related

C/C++ :: Take In Unsigned Number And Afterwards Return N-th Member Of Recursive Function

Mar 14, 2015

I'm having an issue with the following assignment:

Program is supposed to take in an unsigned number and afterwards return the 'n-th' member of the following recursive function:

f(1) = 1, f(2) = 2; f(3) = 3; f(n+3) = f(n+2) + f(n+1) + f(n), n>0;

When I worked with Fibonacci it was pretty easy since I just had to decrement the next member for each step. I used the following:

#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int fib(int n) {
static int memorize[MAX] = {1,1};
if(memorize[n])
return memorize[n];

[Code] ....

My main problem is that I have no visual of the current function, as well as the fact that it takes the f(n+3) = f(n+2) + f(n+1) + f(n), whilist I've only got f(n) to begin with.

View 5 Replies View Related

C++ :: Recursive Function - Take Number And Return With All Sevens Turned Into Eights

Apr 16, 2013

Task: Write a recursive function `seven_up` which takes a number and returns that number with all the sevens turned into eights.

- Example

cout<<seven_up(777)<<endl; //prints 888
cout<<seven_up(1234567890)<<endl; //prints 1234568890
cout<<seven_up(50)<<endl; //prints 50

- Hint: It's like removeFirst, except:

0. Base Case: If the number is one digit long, we don't want to erase it (by returning 0). Instead, riddle me this: When I have a one-digit number, what happens if I change all the 7's into 8's? Well, if the number is 7, it becomes 8, but otherwise it's unchanged.

1. Recursive Call: If the number is longer, then we strip off the last digit, figure out what the answer for the rest is (the recursive call on n/10), and then put the last digit back on the number when you're done.

Hint: You probably need to store the least digit (the n%10) and check if it's 7 separately.

#include <iostream>
using namespace std;
int removeFirst(int n) {
if(n<10)
return 0;
return 10*removeFirst(n/10)+n%10;

[Code] ......

I tried other algorithms but i don't get it.

View 14 Replies View Related

C :: Srand Function Make New Numbers Every Time On Calling

Mar 6, 2015

How do i make the srand() function make new numbers every time i call it? Now it seems like it repeats its result over an over again.

Below is a program that should generate random numbers for 3 dices and print these out. But it prints out the same nr over and over.

Code:
#include <stdio.h>
#define MAX 100
int filler(int dice1[MAX], int dice2[MAX], int dice3[MAX]) {
int throws, nr;
printf("Define the number of throws");
scanf("%d", &kast);

[Code] .....

View 6 Replies View Related

C++ :: Make A Function To Return Placement Number

Mar 25, 2014

I have a class called items and stored in a vector as vector<items*> item.now I want to make a function to return the placement # that the item is stored at with a unsigned int. but if I don't find the item is there like a null unsigned int that can be returned.

unsigned int someFunc(vector<items*> item)
{
unsigned int size = item.size();
for (unsigned int i = 0; i < size; ++i)
{
if (item[i]->getItemName() == "sword")
{
return i;
}
}
//i need code here incase the item is not there
}

because the item might not be there and i would like to do a line if(unsigned int) do this code

View 19 Replies View Related

C++ :: Triangle In Nested While Loop Using Counters

Mar 13, 2013

How to make triangles using c++, in nested while loops.

The triangles were:
*
**
***
****
*****
******
*******
********
*********
**********

I have made a code for this:

int counter=1;
while (counter<=10){
int counter2=1;
while (counter2<counter){

[Code] .....

I was not quite sure about this one.

View 5 Replies View Related

C :: Print Triangle Pattern Using One Loop And Recursion?

Dec 18, 2013

Q.print triangle pattern using one loop and recursion

eg: 5

Code:

#include <stdio.h>#include <conio.h>
void pattern(int n,int N) {
int i,c=1;

[Code]....

View 8 Replies View Related

C++ :: Sum Of The Biggest Numbers In A Triangle

Jan 3, 2014

After trying to understand the problem i finally gave up. The problem requires me to calculate the sum of the biggest numbers in a triangle.

For example: 5
4 0
3 8 2
2 7 9 6

Here is the solved problem from the book(It uses recursion, but i do not get it at all)

#include <iostream>
#include <fstream>
using namespace std;
int triunghi[50][50], n, sum=0;
int suma_max(int i, int j);

[Code] .....

I doesn't give me the same result(26) if a change the order of the numbers in the triangle.

View 3 Replies View Related

C :: Drawing Hollow Triangle Given User Input Indicating Number Of Rows

Oct 4, 2014

How to draw a hollow triangle given a user input indicating the number of rows.

Eg:
Number of rows = 4
___*
__*_*
_*___*
*******

My idea was to split up the triangle into three parts (first row, middle part, last row) and I've managed to write some code on printing the first and last row of the triangle

Code:
int main() {
//Declaring the variables.
int rows, position;
//Prompts user to enter number of rows.
printf("Enter the number of rows in the triangle: ");
scanf("%d", &rows);

[Code] .....

What to do so that I can print the middle part of the triangle. All I know is that I need to use loops. I've also been told that drawing a flow chart would work but I really don't know how to even begin with a flow chart then transform it into code.

View 2 Replies View Related

C++ :: Void Triangle - Print Class Function

Sep 1, 2013

//Point.cpp
#include "Point.h"
#include <iostream>
#include <cmath>
using namespace std;

Point::Point() { //Initialise the point to the origin.

[Code] ....

void Triangle::print() { //print out the Triangle with the format "( (x1, y1), (x2, y2), (x3, y3) )"

How do I accomplish this? When i test with cout << _point1.print(), there's an error:

[Error] no match for 'operator<<' in 'std::cout << ((Triangle*)this)->Triangle::_point1.Point::print()'

View 4 Replies View Related

C :: Calculate Hypotenuse Of Right Angled Triangle - Function Does Not Take 0 Arguments

Mar 9, 2013

error C2660: 'CalculateHypotenuse' : function does not take 0 arguments

Code:
#include"header.h"
#include<stdio.h>
#include<conio.h>
#include<math.h>

[Code] .....

View 6 Replies View Related

C++ :: Translating Recursive Function Into Iterator Function

Nov 27, 2014

How do I change the following recursive function into a iterator function and basically what should it look like?

int f(int i) {
if(i<2)
return i;
return f(i-2)+f(i-1);
}

View 1 Replies View Related

C++ :: Changing Recursive Function Into Iterator Function?

Nov 29, 2014

I had the following question in my exam and received 3 out of a possible 4 marks

Here is the question:

Assume the following recursive function:
int f(int i)
{
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

Translate this function into an iterative version:

Here is my solution:

int f(int i)
{
int prev,next,final;
prev = 0;

[Code]....

View 9 Replies View Related

C/C++ :: Changing Recursive Function Into Iterator Function

Nov 30, 2014

I am trying to translate the following recursive function into its iterative function:

int f(int i)
{
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

This is what I tried to do:

int f(int i)
{
int prev,next,final;
prev = 0;
next = 1;
if(i==1)
final = i;

[Code] .....

View 1 Replies View Related

C/C++ :: Changing Recursive Function Into Iterator Function?

Nov 30, 2014

I am trying to translate the following recursive function into its iterative function:

int f(int i) {
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

This is what I tried to do:

int f(int i) {
int prev,next,final;
prev = 0;
next = 1;

[Code] ....

View 1 Replies View Related

C :: Make A Program Using While Or For Loop?

Nov 20, 2014

I'm trying to write a program for a lab, I know how easy it is but cant see to crack it.

I need to make a program using while or for loop.

I must have the user type a number, for an input value an output value but then do it again several times but without it going on forever asking again and again.

View 4 Replies View Related







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