C/C++ :: Recursion With Printing Strings?
Oct 20, 2014
I am trying to print an outline. My code works up to a depth of 3. (The depth is the number of subsections - so 3 would be section 1, section 1.A, and section 1.A.1). It also works for a width (number of sections and each type of subsection) of 26, where it is capped. However, to get a larger depth, it would involve many more loops. Not only is that terrible code, it also freezes up the terminal I'm working on. I believe recursion would make it much nicer, but I'm struggling to grasp the idea when using a string (I understand when it is a number).
#include <stdio.h>
int sec(int width, int snum) {
char section[100];
sprintf(section, "Section ");
[Code].....
View 1 Replies
ADVERTISEMENT
Apr 26, 2014
This code i made divided a user input into characters and non character. The problem im having is that if a mixed sentence is created, such as 32B, it will only print the 'char string' and not the 'non char string'. But when the sentence is just non characters like 32 it will print the 'non-char string'. So essentially if a mixed sentences is created both of the strings won't be created or printed.(This is only a function by the way).
Code:
Count_chars( char Input[]) {
int i;
for(i = 0; Input[i]; i++) {
if((Input[i] <= 122) &&(Input[i] >= 97)){
printf("Char %c ",Input[i]);
Char[i] = Input[i];
[Code]...
View 4 Replies
View Related
Feb 26, 2015
I need a program that reads the number of lines of a file and then several (max of 20) lines of text from a .txt file so an example of the .txt file is: 2 This is the first string This is string number 2 So it first reads the 2 and then reads the two lines of text. It stores the text in an array of pointers. The code i have so far is:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
[Code].....
It doesnt give me any errors but all it does is keep running and doesnt print anything out kind of like its in an infinite loop although i dont see how that could be possible.
View 1 Replies
View Related
Feb 17, 2014
I am trying to print the array of string using this code but the compiler is printing null in the output:
The code is as follows:
char *arr[10];
int i;
for(i=0;i<10;i++)
scanf("%s",*(arr+i));
for(i=0;i<10;i++)
printf("%s
",*(arr+i));
View 3 Replies
View Related
Feb 23, 2015
I have been skimming and searching but dont know how to word the search just right to find what I need to know. I have written simple stuff with the support of tutorials like weight conversion or loops counting up to a set number. Nothing amazing. The other day I found out how to input and print a string rather than a single text character which i though was bad ass. I even got it to read multiple strings on a single line and found a way to read multiple lines. I can even format it to read both integers and characters on the same line as long as I know the predefined format.
On to my question... How do I read multiple lines with both carecters and integers. for instance:
nissan 1996
toyota 1998
or more comples like
nissan gtr 1996
toyota markii 1998
I want to use
int year;
char make[10]; maybe need to use char make[10][10]; for an array i would guess.
char model[10]; optional for the extra data
but reproduce what i read in a different order. say...
1996 nissan
1998 toyota
vice the original format.
this is what I have tried.
Code: scanf("%s %s", &make,&year);
//The way I seen to read multiple lines was on here
scanf("%[^/t]", %make);
But this wont let me separate the two into two differnet definded data types. Let alone use printf to display them in reverse order.
View 1 Replies
View Related
May 12, 2014
To construct and write down algorithm of determination of the sum of squares of consecutive integers with recursion use. I tried to do something:
public static int RecSumSquare(int x, int n)
{
if (n < 0) throw new ArgumentException("n should be greater than zero");
if (n == 0) return 0;
else return x*x+RecSumSquare(x, n - 1);
}
But I don't know as the beginning and the end of this algorithm will look.
View 2 Replies
View Related
May 4, 2014
Suppose I'm given the following code:
int mystery(int n){
int something;
if(n > 0)
View 1 Replies
View Related
Mar 27, 2013
So the task is to find the node with minimum value of a binary tree (not binary search tree). the input is the pointer to the root of the tree. and i cannot make recursion work when i do if conditions. here is what i have Code: /*function 3-takses as input the pointer to the root of the tree and returns a pointer to the node with the minimum value*/
CPPtr minimumvalue(CPPtr SP){
CPPtr min = NULL; //node of minimum value
if(SP== NULL){ // if there is a node, begin comparing
return NULL;
}
else{
if(SP->data<SP->left->data){ //if the node has smaller value than its left child
min = SP; //update node of minimum value
[code].....
no matter where i call my function i get errors like unhandled exception at some memory. how to use recursion in this?
View 6 Replies
View Related
Feb 8, 2013
I have this problem set that has to use recursion to permutate scores. I have read in all the numbers and have tested that it works . l have also attached what I have so far. Here is what I think I need to do:I have to pass the structs into this permutation algorithm that is here:
Code:
#include <stdio.h>
void ListPermutations(char str[]);
void RecursivePermute(char str[], int k);
void ExchangeCharacters(char str[], int i, int j);
[Code]....
View 1 Replies
View Related
Apr 28, 2013
It seems that boost's file recursion requires that the file using the recursion must be in the include path. This makes using file recursion in a library header a problem as libraries may be located in a subdirectory of an include path (which is minor since the programmer can state the subdirectory in the recursive file call as long as it is not relative to the calling file). I also found it a problem when the main compile directory isn't in the compile path.
View 9 Replies
View Related
Mar 28, 2013
So the task is to find the node with minimum value of a binary tree (not binary search tree). the input is the pointer to the root of the tree. and i cannot make recursion work when i do if conditions. here is what i have
CPPtr minimumvalue(CPPtr SP){
CPPtr min = NULL;//node of minimum value
if(SP== NULL){// if there is a node, begin comparing
return NULL;
[Code] ....
No matter where i call my function i get errors like unhandled exception at some memory. How to use recursion in this?
View 1 Replies
View Related
Oct 15, 2014
I want to convert the following program into a non-recursive code using a stack and a loop:
void write(int n) {
if (n>0) {
write(n-1);
cout << n << " ";
write(n-1);
}
}
And here is the code I am using but that currently does not work, and I do not know why:
stack<int> S;
S.push(n);
while (not S.empty()) {
int k = S.top();
[Code] .....
How to simulate that recursion. I thought it was enough to push into the stack the equivalent recursive call.
View 2 Replies
View Related
May 8, 2014
So i have a program it works which takes in a word reverses it prints it out. BUT! the problem is that the program is without recursion. convert the function into recursion.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
[Code].....
View 2 Replies
View Related
Nov 9, 2014
I put some checks in factorial program, I am confused how 27, 40 50 is coming in output and ans is 10.
#include<stdio.h>
int fact(int n) {
if(n==1) {
printf("hello1 %d
",n);
return 1;
[code].....
View 2 Replies
View Related
Aug 28, 2013
I have a linked list I made and am practicing a simple recursion function sum() which returns the sum of all numbers in each node of the list. Here is my sum() function:
int sum(ListNode *ptr){
if (ptr->next)
return (ptr->num + sum(ptr->next));
else
return(0);
}
where the initial arg passed through the ListNode * parameter is my head node (with null value). The output is fine until that point, then the program locks up and I get the "...has stopped working" error.
View 1 Replies
View Related
Apr 28, 2014
I have been asked to write three functions for a Liked List that can add remove and reverse the elements of the Linked List. Now, I have done the add function and It is displaying the elements properly using recursion function. But My removal function is not working and I tried all the possible logic I can think of, I dont know, where did I make mistakes.
This is My class for the Linked List
#ifndef __Linked_Lists_Recursive_function_Implementation___NumberList__
#define __Linked_Lists_Recursive_function_Implementation___NumberList__
#include <iostream>
using namespace std;
[Code]....
View 4 Replies
View Related
Feb 7, 2015
I am trying to input a recursion method. The code compiles, however, it is only giving me a value of 1. I am wanting the value of 5 when it is compiled. Why is this?
#include <iostream>
using namespace std;
int number(int x) {
if (x == 1)
[Code] ....
View 9 Replies
View Related
Mar 20, 2013
How to print to the screen the value of n after it has been multiplied.
For example: if I use cout << "n: " << n << " power: " << power << "
"; I
can see the variable "power" decrementing by 1, but I don't see the variable "n" incrementing with its new value after it has been multiplied by n * n.
#include <iostream>
using namespace std;
typedef unsigned short int USHORT;
typedef unsigned long int ULONG;
ULONG Getpower(USHORT n, USHORT power);
[Code] .....
View 2 Replies
View Related
Feb 25, 2013
I'm trying to write a program that converts a decimal number to a binary one. I have most of the program written, but I am having a little bit of trouble. Whenever I enter a decimal number, the program will convert it correctly to binary, however, the last number is not included in the conversion. EX: Converting 37 into binary (0100101) yields 010010 when entered into the program. BTW the program must utilize recursion to achieve this goal.
#include <iostream>
using namespace std;
void decToBinary(int num1);
int main() {
int num1;
[code]....
View 4 Replies
View Related
Dec 29, 2013
I need to sort an array of n given elements using recursion. What am i doing wrong here?
#include <iostream>
using namespace std;
int i=0, j=1, v[100], n;
[Code].....
View 2 Replies
View Related
Apr 3, 2013
How do function count factorials in this program?
#include<iostream>
#include<conio.h>
using namespace std;
void main(){
unsigned int factorial(unsigned int value);
[Code] .....
View 7 Replies
View Related
Oct 23, 2013
write a code that draw a block and diamond each in a single run by using '*' in recursion.
Below are one 10X10 rectangle and 6X4 diamond.
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
**
* *
* *
* *
* *
* *
* *
* *
**
Note: I couldn't write the diamond here appropriately somehow.
View 1 Replies
View Related
Apr 17, 2012
I am having to write a program that takes 2 sorted linked lists (x and y) and merge those to sorted into a third one (z). This has to be done via recursion. I think I am close but there is a logic error.
Code:
void SortedMergeRecur(Node*& xHead,Node*& yHead,Node*& zHead)
{Node* temp = 0;
if(xHead == 0) {
zHead = yHead;
yHead = 0;
}
[Code]...
View 3 Replies
View Related
Oct 24, 2014
This function is apparently causing infinite recursion, but I have no clue what's causing it:
Code:
int pow( int base, int exp ) { int somevariable = pow(base,exp-1);
if (exp == 0) {
return 1;
}
else {
return base * pow(base,exp-1);
}
}
View 10 Replies
View Related
Mar 6, 2015
I am trying to reverse a Linked list using recursion but whenever I try to print it after reversal it prints only the last element of the list.
Code:
void reverseRecursive(Node **root, Node *temp)
{
Node *next = temp->next;
if(temp->next == NULL)
{
*root = temp;
[Code]...
I have just posted the reverse function to enable easy readability rather than post the entire code
View 7 Replies
View Related
Jun 19, 2013
I wrote the following recursion for finding the distance between a root and a leaf, but am not quite sure how to make it work.
Code:
int calc_depth(Node* tree, Node* zero) {
if (!tree)
return 0;
if (zero->right || zero->left == tree)
return 1;
return (zero->right ? calc_depth(tree, zero->right) : calc_depth(tree, zero->left));
}
View 14 Replies
View Related