Answered # homework ques: use recursion to insert comma into integer.

Discussion Starter DuffManLight WaltP 2,905 Discussion Starter DuffManLight WaltP 2,905 Discussion Starter DuffManLight WaltP 2,905 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

0

Trying to learn recursion. Problem asks to use recursion on non-negative number to insert commas. Ex: 20131 as 20,131. My program adds in an extra comma at end. Any help in pushing me in the correct direction to fix this would be appreciated.

Tried to use a 'count' to determine end of number but can not figure it out.

input: 546789301

output: 546,789,301,

```
#include "stdafx.h"
#include <iostream>
using namespace std;
void commaInsert(int v, int &count)
{
if (v <= 0)
{
count++;
cout<<"Count equals: "<<count<<endl;
return; //base case
}
commaInsert(v/1000, count);
cout<<(v%1000)<<((count=1)?",":"");
return;
}
int main()
{
int input = 546789301, count = 0;
commaInsert(input, count);
cout<<endl;
return 0;
}
```

*Edited 6 Years Ago by WaltP*: Don't use TEX mode. Just type your question.

0

Sorry about text screw up. Original question:

Trying to learn recursion. Problem asks to use recursion on non-negative number to insert commas. Ex: 20131 as 20,131.

My program adds an extra comma at end. Any help in pushing me in the right direction would be appreciated.

Tried to use a 'count' to determine end of number, but can not get it working.

input: 546789301

output: 546,789,301,

0

You always output the 3 digits followed by ','. Assume you have 10. Follow the code with that value to see why the comma is output.

0

Walt,

I know why the comma is being output. I don't know how to stop it from being outputted on the last iteration. Maybe my recursion function can not be corrected with the current design? Recursion is difficult and I am looking for some pseudo code ideas to correct my current design. Thanks in advance for your time.

0

As you obviously know, you divide out the last 3 digits and call the function again.

When you divide out the digits and your source is 0, you have the highest value as a result (the 45 from 45,652,567)

Output it and return.

The return loop then outputs ',' followed by the precious result.

So a minor change to you function should get it. Think `if-else`

when you get your result.

0

Thanks again Walt. After much time playing around with it I got it working. For any other beginner assigned this puzzle puzzler, here is a working code sample to review.

```
#include <iostream>
#include <iomanip>
using namespace std;
void commaInsert(unsigned v)
{
//base case
if (v <= 0)
{
return;
}
//main logic
commaInsert(v/1000);
if(v >1000)
{
cout<<",";
cout<<setfill('0')<<setw(3)<<(v%1000); //stores on stack waiting for return
}
else
cout<<(v%1000); //stores on stack waiting for return
return;
}
int main()
{
while (true)
{
unsigned input;
cout<<"Enter an integer to process: ";
cin >> input;
if (input == 0) //special case
cout<<"0"<<endl;
else if (input == 1000) //special case
cout<<"1,000"<<endl;
else
{
commaInsert(input);
cout<<endl;
}
}
return 0;
}
```

0

Good job. But why is 1000 a special case? And 0?

Here's my solution, in C:

```
#include <stdio.h>
void outNumber(int num)
{
int part;
if (num < 1000) // Last segment
{
printf("%3d", num); // output it and return
return;
}
part = num % 1000;
num /= 1000;
if (num > 0) outNumber(num);
printf(",%03d", part);
return;
}
int main(int argc, char *argv[])
{
int num = 23456; // Default this number
if (argc > 1) // Allow a number from the command line
num = atoi(argv[1]);
outNumber(num); // Call function
return 0;
}
```

Note there are no special cases.

*Edited 6 Years Ago by WaltP*: n/a

This question has already been answered. Start a new discussion instead.

Recommended Articles

Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.

This is the result of previous question answered :

code for the listbox - datagridview interaction

At the top of the code ...

the function that I created to find the ...