C :: Array Sorting Program That Works With Recursive Function

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


ADVERTISEMENT

C :: Way Recursive Calls Actually Works

Apr 27, 2013

This is simple recursive solution of Fibonacci number:

Code:

int fibo(int n)
{
if(n<=1)
return 1;
else
return fibo(n-1)+fibo(n-2);
}

Now the recursion will generate a large recursion tree, like if n=5, 5 will call (5-1), (5-2) or 4,3 . What I want to know is, will fibo(n-1) will be called 1st go all the way to the base case 1, then do the summation or fibo(n-2) will be called right after fibo(n-1) ?

View 6 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++ :: Seg Fault In Recursive Sorting Algorithm

Apr 23, 2015

So I am working on a dual pivot quicksort, I can correctly sort the array the first time around however in the main it is called again on the sorted array and in the function I get a seg fault at line 17 in sort.cpp, for the life of me I cant figure it out as the values are the same when I pass it in the first time. Here is the code:

main1.cpp

#include <iostream>
#include "movement.h"
#include "sort.h"
using namespace std;
int main() {
const int size = 10;
T array[size] = {6, 5, 1, 8, 4, 7, 2, 9, 6, 3};

[Code] ....

View 4 Replies View Related

C :: Reverse Array Of Characters Using Recursive Function?

Mar 15, 2014

Eg. User input : abcde
Program Output : edcba

I keep on getting weird ASCI symbol in return, I couldn't achieve what I need, and tried debugging for days,

Code: char ar[20];
int len,n=0;
printf("enter the string to be reversed :
");

[Code].....

View 5 Replies View Related

C++ ::  Recursive Palindrome Test With Merge Sorting

May 28, 2013

My assignment is to make a program that tests whether something is a Palindrome or not using a recursive function. Also, in order to test what type the Palindrome is (if it is indeed a palindrome) I'll need to merge sort it. Now alphabet characters, spaces, and numbers are all allowed, as long as the spaces line up with the spaces in the original input. And yes this is user inputted. I can show you what I've got so far and then I'll tell you what my problem is.

#include <iostream>
#include <istream>
#include <string>
#include <string.h>
#include <conio.h>
using namespace std;
/************To Test if Palindrome*******************/
bool IsPalindrome(char *str, int size) {
if( str[0] == str[size-1]);

[Code] .....

Now my problem is that I can't seem to get the test to work correctly. Since the word is user inputted I don't have any way of knowing the size so that kind of complicates it a little bit. The other thing is I was told by my professor that I need a boolean flag to make it work, and I need to set the value to the return of the IsPalindrome function. I'm not sure how to do that. I also don't haven't been able to add in the MergeSort yet, so the Order and Type don't really work correctly because I'm not sure how to get int len for it. (len is length)

View 3 Replies View Related

C :: How Getline Function Works

May 15, 2013

How the getline function works.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {

[Code] ......

getline(&inputstr, 128, NULL); getline gets a line in a data file.

I assume that inputstr[128] is the name of the file? why is the number of the array in the getline function....

View 7 Replies View Related

C :: Program Works With Float But Not Double?

Sep 8, 2014

I tried writing a program for finding the area of a circle, and I'm wondering why it only works when i define it as 'float', but not as a double?

Code:
#include <stdio.h>
#define pi 3.14159
double
main(void)

[Code] .....

View 3 Replies View Related

C :: How Program Works If Integer Is More Than One Digit

Mar 15, 2013

Assignment: Take an integer keyed in from the terminal and extract and display each digit of the integer in English. Ex. 932 --> nine three two

Code:

/*This program takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English.*/
#include<stdio.h>
int main(void)
{
//DECLARE VARIABLES
int num;
}

[code]....

I don't know how the program works if the integer is more than one digit.

View 1 Replies View Related

C :: Passing Array Of Struct To Sorting Function

Mar 6, 2015

how is this done not really sure .

Code:
//prototype
void improvedbubblesort (struct team Table[]);
//this is defined out side of main

[Code]....

the whole code is very long but can post if necessary . Basically i get no errors in this but its not sorting them . Think its in the way i pass the array of struct .

View 3 Replies View Related

C++ :: Program Works In Release Mode Not In Debug?

Feb 20, 2013

i've got an assigment that requires me to overload some operators and add some objects together.

I will show the code and explain as good as I can.

void SArray::operator+= (const SArray &obj)
{
Sphere * tmp_arr;
tmp_arr = new Sphere[obj.antalobjekt+this->antalobjekt]; //antalobjekt = //Gets amount of elements in the arrays.

[Code]......

m_arr is the inner array for storing elements, do ask if something is not clear enough. The copy constructor works, so i have not included it.

View 1 Replies View Related

C++ :: Only The Last Element Of Array Works

Oct 5, 2014

My code has been acting odd. I made a function that layers my art resources but only the last tile of my art resource acts the way it should. My character goes behind and in front of the last tile and gets printed correctly. Strangely its really exclusive to the last tiles I print. What I need is to figure out in step by step order what going on with my code sample below and be able to layer the resources.

Here is a small sample of my main function. This is how I do my rendering.

Code:
Int main (int arc, char* args[]) {
//Move class
Move character;
//Class Tile & Side Tile
Tile *tiles [TOTAL_TILES];

[Code] ......

View 14 Replies View Related

C :: How Does Malloc Works On Array

Dec 15, 2013

i want to ask how does malloc works on an array for instance can i dao this

Code: p=100;
int array[10];
array = malloc(p * sizeof(int));

then will the size of int be 100 so will i be able to do like

Code:

array[11] = 12 ; ???

also if i have a 2d array how can i use malloc on it and how does this works with pointers????

View 2 Replies View Related

C :: Program Works Primarily By Receiving User Input

Jun 24, 2013

My program works primarily by receiving user input however; using 'char' and 'fgets' i have to stipulate how many characters i want assigned, and this isn't practical for what I am after. Example below.

Code:

char example[50];
printf("What colour is the sky?
");
fgets(example, 50, stdin); Is it possible that the assigned number (in this case '50') is determined strictly by the user input?

for example, user input is Blue then 50 is then 5?

View 9 Replies View Related

C++ :: Function Works When Passed String Literal But Not A Variable

Jul 10, 2014

I'm making a .json loader for a project that I'm working on, and to simplify things, I decided to make all the root attributes named in a separate file. Here's my problem: my loading function, Add(const char* id), works just fine when I pass it a string literal.

However, when I use a function that iterates through a vector list, even with the exact same definitions as the literal, it returns the error: std::out_of_range at memory location 0x0026fb30

I've stepped through it with the VS2010 debugger about a hundred times, checked against memory locations, and just have done everything I can think of, all to no avail..

The code, with data-checking omitted:

The std::map I'm adding to:

static std::map<const char*, Item*> *s_mItems;
Initialized as std::map<const char*, Item*> *Item::s_mItems;

Add() Function (Works by itself with a literal):

static bool Add(const char* id) {
...
std::string name = node.Get("name").ToString();
std::string desc = node.Get("description").ToString();
int rarity = StrToRarity(node.Get("rarity").ToString());

[Code] ....

AddList() function, where the program always breaks:

static void AddList(std::vector<std::string> list) {
for(std::vector<std::string>::iterator it = list.begin(); it != list.end(); it++) {
Add(it->c_str());
}
}

View 3 Replies View Related

C++ :: Creating Stand Alone Function That Works With Member Functions?

Nov 1, 2014

I was able to get this program running. Now I working on taking one of my member functions and turning it into a standalone function. I choose the create_board() function. Yet, if I declare it in my header file or my main.cpp it cant access any info from the original member functions?

main.cpp
#include "ticTacToe.h"
#include <iostream>
#include <limits>

[Code]....

View 2 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 :: Recursive Crashes Program?

May 13, 2014

cause I cant find why it crashes. It compiles without any error,but crushes when i run it and I can't find where is wrong the code.

Code:

#include <stdio.h>
#include <time.h>
#include <math.h>

[Code].....

View 7 Replies View Related

C :: Feedback On Recursive Function

Nov 16, 2013

The recursive function is bolded, i got feedback and was told that the static variable made the function seem a lot like a iterative function but he did not say why.

Code:
#define MAX 100
#include <string.h>
#include <stdio.h>
int checkPalindrome(char string[MAX]);
int checkRecPalindrome(char string[MAX]);

[Code] .....

View 7 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 :: Recursive Function Not Working

Nov 9, 2013

the functions checks if the word is a palindrome like"level" "madam" etc. but with input "dfdfdfdffdfd" my recursive function fails.

Code:

/* main.c - Created on: Nov 9, 2013 - Author: Kaj P. Madsen*/
#define MAX 100
#include <string.h>
#include <stdio.h>
int checkPalindrome(char checkString[MAX]);
int checkRecPalindrome(char checkString[MAX], int strLgt, int a);
}

[code]....

results from "dfdfdfdffdfd" added some print to see that its the variables a and strLgt not functioning properly

Code:

dfdfdfdffdfd.
The word is not a palindrome(iterative)
strLgt: 11 a: 0
a: d strLgt: dstrLgt: 10 a: 1
a: f strLgt: fstrLgt: 9 a: 2
a: d strLgt: dstrLgt: 8 a: 3
a: f strLgt: fstrLgt: 7 a: 4

The word is palindrome (recursive)

View 4 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 :: How To Print Out Recursive Function In Main

Feb 9, 2013

I am just practicing some recursion and I am having trouble with printing out a recursive function in main. Here is what I have:

Code:

// This function adds the squares 4, 5 = 4*4 + 5*5 recursiveley
int recursive_sumSquares(int m, int n) {
if (m < n) {
return m*m + recursive_SumSquares(m+1, n);
}
else {
return m*m;

[Code]...

I am getting an error that says undefined reference to 'recursive_SumSquares'

View 2 Replies View Related







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