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
ADVERTISEMENT
Feb 2, 2015
My program uses a while loop to eventually get to an error of zero and a root of sqrt(3). I'm not understand why after the third iteration the program fails to compute a new x value. I'm using Visual Studio 2013. The code tag instructions were dubious.
Code:
#include <stdio.h>
#include <math.h>
main() {
/*This program uses the Newton-Raphson method to solve y = (x^3)-3 for it's roots.*/
printf("This program uses the Newton-Raphson method to solve y = (x^3)-3 for it's roots. Enter your estimate of the root.
");
float x,y,z;
int num;
num = 0;
[Code]...
View 1 Replies
View Related
Nov 26, 2013
int Fib1 = 1;
int Fib2 = 2;
int Fib3 = 0;
int randomynumber;
int Loop;
[code].....
this returns negative numbers sometimes.what did i do wrong side note this is not the complete program it is only the part with the problem because the complete code is sort of longish and very confusing
View 2 Replies
View Related
Aug 27, 2014
Having error . I multiplied 0 by -4 and my result is -0 instead of 0. I tried to change the data type put It won't work. This is my code:
#include <iostream>
int main () {
double b, c;
std::cout<<"b: ";
std::cin>>b;
std::cout<<"c: ";
std::cin>>c;
std::cout<<b*c<<std::endl;
return 0;
}
View 2 Replies
View Related
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
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
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
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
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
View Related
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
Mar 10, 2013
i want to write an array sorting program that works with recursive function.but i can not do it.
my algorithm
Recursive
find the min value
find the min value and save it
find the min value and save it and remove it from loop
put the rest in loop again
find the min value again
..
...
i didnt write the function because i dont know how will it work
Code:
#include<stdio.h>
#include<stdlib.h>
#define s 5
void main() {
int a[s]={25,3,2,4,1},c[s]; // c[s] for new sorting
int i,ek,j; //ek = min
[Code]....
View 7 Replies
View Related
Oct 30, 2014
while(!secList.empty()){
Security se = Security();
se = secList.extract(); // CRASH
cout << "Security info: " << se.security << endl;
cout << "Transaction List: " << endl;
while(!se.tranList.empty()){
Transaction tr = Transaction();
[code]....
my program crash when it try to assign the return value of the function to the local value. extract function does return correct value, but it just crash when done executing.
View 4 Replies
View Related
Sep 3, 2014
I nead to define an a negative number a normal posetive number i defined like #define RSSI_UP 1 can i write #define RSSI_DOWN -1???
View 2 Replies
View Related
Sep 30, 2013
The code on lines 44-53 is suppose to display a message when the user enter a negative number, however, when a correct positive number is entered the message is display again.
#include<iostream>
#include<cctype>
using namespace std;
int main() {
char carType;
int A, B, C;
[Code] ....
View 2 Replies
View Related
Mar 12, 2014
I am getting strings from an HTTP request that will have hex values and I must convert those strings to a signed decimal.
//typical string inside response: //0E1D052BFBB711C1002C0042007A014DFE44022B270F7FFF8000000000000000
//every 4 characters above are a signed decimal value
for (a = 0; a <= 63; a+=4){
sprintf(vval,"0X%c%c%c%c",response[a],response[a+1],response[a+2],response[a+3]);
ds = strtol(vval, NULL, 16);
sprintf(vval,"%d",ds);
}
The problem is I never see a negative number. Decoding 0x8000 gives me 32768 but not -32768.
View 7 Replies
View Related
Mar 3, 2014
In this exercise:The C Programming Language Exercise 3-4..It states the following: "In a two's complement number representation, our version of itoa does not handle the largest negative number, that is, the value of n equal to -(2 to the power (wordsize - 1)) ."
A char is one byte (255 bits). The range of an 8 bit variable using a two's complement representation is -128 to 127. Therefore -128 is the largest negative value. The statement in book suggests that the itoa function will not output -128 if we pass -128 as a parameter, because in itoa when we try to convert -128 to positive -128, the inverse of -128 is -128. However, I just ran this code in my computer and it successfully outputted -128.
Code:
#include <stdio.h>
#include <string.h>
#define SIZE 10
void reverse(char s[])
{
int c, i, j;
}
[code].....
View 2 Replies
View Related
Dec 6, 2013
I receive a negative number but in big endian order. ntohl seems work for unsigned only.
Is there a method for me to translate it back to the original negative number from big endian?
View 2 Replies
View Related
Oct 21, 2013
I have a error with one of my programs. I'm supposed to get rid of negative numbers when there are numbers that are randomly generated. Here is the middle part of the code.
{
int vectorLength = 10;
vector<int> bothSigns(vectorLength);
cout << " Input vector: ";
for (int i = 0; i < vectorLength; i = i + 1)
{ bothSigns[i] = rand()%201 - 100;
[code] .....
The part where i'm supposed to start is after the /////'s. However, whenever I input a number for the random numbers(not put in part of code), i keep getting a segmentation error.
View 3 Replies
View Related
Jul 15, 2014
I am trying to find the max number entered by the user, and it should terminate when a negative number is entered. For my code, it will just end when the user inputs a lower number than the previous. i.e.- 10 20 15 "The highest number is 20" when it should be "10 20 5 40 15 -1" "The highest number is 40". No arrays or do/while loops either.
#include <iostream>
using namespace std;
int Max(int x);
int main() {
int x;
[Code] ....
View 9 Replies
View Related
Nov 10, 2014
So I tried creating a test code for reading a negative number and positive number but whenever I enter a negative number it read it as being positive.
#include <stdio.h>
#include <iostream>
#include <iomanip>
[Code].....
PS: I am using char over int because the program that I am testing for requires me to use 8 bit variable.
View 2 Replies
View Related
Feb 3, 2013
here's one more thing id like to do to make the input even better able to handle user error, but im not sure if its possible or at least easy. I need the function to return a large positive number. As of right now, it can handle users entering characters, but what if the user enters a negative number? is there a way to check to see if what is coming in is negative before the sign gets lost in conversion to unsigned"ness"?
Code:
unsigned long getNum(char prompt[80])
{
unsigned long darts;
printf("%s", prompt);
while((scanf("%lu", &darts)) != 1)
{
[code]....
View 3 Replies
View Related
Jul 24, 2014
I am given an array with n elements but need to write a function where it returns n-1 elements. Do I need a loop for this? Or must I write a prototype...
Here is what I have thus far:
//given array with 5 elements function must return value 4 elements since -1 is a special character length of list is finite
#include <iostream>
using namespace std;
int main () {
int array [] = {1, 4, -1, 3, 2};
cout << "The array has " <<sizeof (array)/ sizeof (int)<< " elements"<< endl;
return 0;
}
View 5 Replies
View Related
Sep 2, 2013
I have a homework assignment due that told me for the "input specification" that "n" is an integer greater then 0. How would I put this in and where?
View 1 Replies
View Related
Jun 30, 2014
A program that asks 10 positive integers and classify them as odd or even positive integer inputs. The algorithm has to trap negative integer inputs. The output will be displayed in two columns. ODD and EVEN.
here's my program code...
#include<iostream>
using namespace std;
main() {
int countA;
for(countA=0;countA<=10;countA++) {
[Code] ....
View 3 Replies
View Related
Mar 6, 2013
I have to write a program that uses negative numbers in a conjecture algorithm. The program terminates when a number is reached that was already outputted. I can not get it to work and I have been racking my brain for a week.
#include <iostream>
using namespace std;
int main() {
int x[1000], y[1000], i=0, j=0, count=0, w=0, z[1000], t = 0;
[code]....
View 2 Replies
View Related
Feb 28, 2015
We are programming a function calculator and my instructor hinted that the add() function is almost exactly like the subtract() function and we would only have to write one function if we "called it in a special way." This got me thinking, can I turn a function's argument to negative during the call to make it subtract?
Example:
void addSub(int &num1, int &num2)
into this
void addSub(int &num1, int -(&num2))
Would this work when passing by reference?
View 9 Replies
View Related