Issue with vectors

Goal of the assignment - user inputs a series of numbers (ex.num=12345) as well as different single digit numbers for d and d2. if d is in the original series of numbers it needs to be replaced with d2 (ex. d=3, d2=9 --> num=12945).
*not allowed to use strings*

my logic - use a loop to pick off the numbers starting from the back. if =x replace with x2. Stuff the values into an array and then cout the array backwards. I created a function that finds the length of the inputted value, but it is in terms of a variable and i'm unable to declare an arrays size using a variable. Therefore I believe that I need to use a vector, which i'm unfamiliar with. My questions are in the comments relating to vectors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <vector>
using namespace std;

int getDigit(int num);
int flipDigit(int num);

int main()
{
    int d, d2, num;

    cout << "What is the series of numbers" << endl;
    cin >> num;

    do
    {
        cout << "What is the number that you want to be switched throughout a series of numbers?(ex.0-9)" << endl;
        cin >> d;
    }while((d < 0) || (d > 9));    
    
    do
    {
        cout << "What is the number that you want your chosen number to be replaced with?(ex.0-9)" << endl;
        cin >> d2;
    }while((d < 0) || (d > 9));
    
    	
    int getDigit(int num); //Finds how many digits are in num
    {
    	int x = num;
    	int result = 0;
        while(x > 0)
        {
			x = x/10;
            result++;
        }
        return result;
	}
		
	std:vector<int> rev(getDigit(num));	//Is this the proper way to declare a vector the size of getDigit?
		
    int flipDigit(int num);	//Reverses the order of numbers while replacing d with d2.
    {
        int x = num;
        int y = num;
        int z = 0;
            
        while(x > 0)
        {
            y = x%10;
	    x = x/10;
                    
            if(y==d)
            {
                y=d2;
            }
            
            rev.push_back(y);  //does this properly put y into the vector?
        }
            
        for(int i=getDigit;i>0;i--)	//How do I set i = to the value returned from getDigit?
		{                       
			cout << rev(i);  //What is the proper way to format the cout of a vector?
		}
    }
return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <vector>
using namespace std;

typedef std::vector<int> Vector;

int getDigit(int num)
{
	int x = num;
	int result = 0;
    while(x > 0)
    {
		x = x/10;
        result++;
    }
    return result;
}

void flipDigit(int num, int d, int d2, Vector *v)
{
    int x = num;
    int y = num;
    int i = 0;
            
    while(x > 0)
    {
        y = x%10;
        x = x/10;
                
        if(y==d)
        {
            y=d2;
        }
        (*v)[i++] = y; 
        cout << x << " " << y << " " << i-1 << " " << (*v)[i-1] << "\n";
    }
}

int main()
{
    int d, d2, num;
    cout << "What is the series of numbers" << endl;
    cin >> num;

    do
    {
        cout << "What is the number that you want to be switched throughout a series of numbers?(ex.0-9)" << endl;
        cin >> d;
    }while((d < 0) || (d > 9));    
    
    do
    {
        cout << "What is the number that you want your chosen number to be replaced with?(ex.0-9)" << endl;
        cin >> d2;
    }while((d < 0) || (d > 9));
    
	int sz = getDigit(num);
	Vector rev(sz);	//Is this the proper way to declare a vector the size of getDigit?

    flipDigit(num, d, d2, &rev);    
    for(int i = sz - 1;i >=0  ;i--)	//How do I set i = to the value returned from getDigit?
	{                       
		cout << rev[i];  //What is the proper way to format the cout of a vector?
	}    
    
    return 0;
}
if needed to use push_back, dont size the Vcctor :

1
2
Vector rev;	
//Vector rev(sz);	 


and then change line 34

1
2
        //(*v)[i++] = y; 
        v->push_back(y);
Last edited on
Extremely helpful, thank you so much.
Just one question - what is the point of line 35?
Last edited on
why use vectors? If we were worried about big nums (and thus had strings) this might be a valid usage, just use the place at each num.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>  // gives cin, cout
using namespace std; // eliminates needing the "std::" prefix

/***************************************************************
    New function prototypes. Include a description of the function
    input(s) and the function output. Often some data is helpful:
             
    int largest( int, int, int );   // largest(4,5,-1) returns 5
***************************************************************/

int flipDigit(int series, int replace, int replacer); //flipDigit(12345, 3, 9) returns 12945

/***************************************************************
    main() function code.
    The main() function always takes zero parameters/arguments
    and returns 0 to indicate that the input was successfully
    processed into output. 
    
    Later: A return of [some other value] means [something else].
****************************************************************/

int main()
{
    int userNum = 0;
    int d1 = 0;
    int d2 = 0;
    
    cout << "Welcome to the number swapper\n";
    cout << "Input the number series: ";
    cin >> userNum;
    cout << "What number do you want switched: ";
    cin >> d1 ;
    cout << "what number do you want to swap it with: ";
    cin >> d2;

    cout << "The number series " << userNum << " with " << d1 << " replaced by " << d2 << " is "<< flipDigit(userNum, d1, d2) << "\n";
    system("pause");   // pauses the screen waiting for the user to hit anything, security issue. replace this with something else
    return 0;           // return 0 to indicate successful execution of main()
}

/***************************************************************
    Code for other functions
    Use the main() comment above as the model.
***************************************************************/

int flipDigit(int series, int replace, int replacer)
{
	
	int base = series; // our base for the series
	if (series < 0) // check to see if input number series is negative
	{
		base = (base * (-1)); // make calculation base positive
	}
	// vars used for various things I like to initialize mine
    int quotient = 0;
    int mod = 0;
    int size = 1; // initialize to one for later
    int output = 0;
    
    while ((base/size) > 10) //while size is down at least one place from the end
    {
    	size = size * 10; //increase size up a space, remember it was initialized at 1
	}

	mod = base;

	while (size > 0) // while we are still working with a place in the num
	{
		quotient = mod/size; // what is the num at the place of size
		mod = mod % size; // mod becomes everything lower than the current place
		if (quotient == replace) // if the num at place is the replacement number
		{
			output = (output + (size*replacer)); // set place to replacement num
		}
		else 
		{
			output = (output + (size*quotient)); // set place to original value
		}
		size = size/10; // go down one place
	}
		if (series < 0) // if it was negative originally we go back to being negative
	{
		output = (output * (-1));
	}	
	return output;
}
Last edited on
Definitely see how this is a better approach because of its simplicity, thanks for the input.
@mmcro : line 35 is just to see what happen in the code. You can delete of course.

@willuwontu : right, but the question was about vector.
Last edited on
Topic archived. No new replies allowed.