Question:
Changing numbers in an array in C++?
✘ ρнια ✘
2011-04-26 05:40:12 UTC
I have an array, let's say this array was

int a = {21, 22, 23, 24, 25}

When a user enters a value, I would like to change the value of a number in the array
So let's say my user enters the number 22. This causes the number 22 in the array to change to **.

int guess;
scanf("%i", &guess);

So how would I check "guess" against the array and how would I change the array?

Secondly, would I be able to do this because ** isn't an interger?

Thanks,
Sophia. :)
Three answers:
Cubbi
2011-04-26 06:30:07 UTC
If you make that an array of strings, the task becomes fairly simple, since this is exactly what the C++ function replace() does.



#include

#include

#include

int main()

{

     std::string n[5] = {"21", "22", "23", "24", "25"};

     std::cout << "Enter a value: ";

     std::string value;

     std::cin >> value;

     std::replace(n, n+5, value, std::string("**"));

     std::cout << "The array is now: ";

     for(int i=0; i<5; ++i) std::cout << n[i] << ' ';

     std::cout <, '\n';

}



test: https://ideone.com/8QXTP



If you need to know if the value is found in the array, use find() first.
koppe74
2011-04-26 13:00:08 UTC
Can't be done. int holds integers, it can't hold a string like "**".



Of course it could hold a single '*' coded in ASCII, but it would be impossible to differentiate between the ASCII-coded '*' and the possibility of the same number being used as a number to guess.



You need to use a value "outside" the ones being guessed, I'd suggest 0 or perhaps a negative number (-1).



Further more, you don't declare an array that way. Use:

int a[] = {21, 22, 23, 24, 25};

or

int a[5] = {21, 22, 23, 24, 25};



Since the first element is the "zeroth" element ( a[0] ), 22 would be a[1] .



To change it to -1 to mark it as "guessed" :

a[1] = -1;



Alternatively, you could make an array of char-pointers ( char * ) as that's what strings are, but then you'd have to convert the strings to numbers before doing any arithmetic on them.
dewcoons
2011-04-26 12:52:19 UTC
Your "secondly" is correct. Since the array is an array of integers, you would not be able to change the value of the array to "**" because that is not an integer.



What you can do is change the value in the array to a predetermined value (such as zero - which is an integer). Then when it comes time to display the results to the user, you tell the program that if the value in the array is zero, it displays "**" on the screen.



How would you change the value in the array based on the input? You would need to use a loop, such as a for or while loop.



for (int count=0; count=4; count++) // 5 being the number of items in your array - but numbers from 0 to 4 not 1 to 5

if a[count] == guess { //Look through each element of the array and see if it is the same as the number guess

a[count] = 0; // if it is, set the array element to zero

}



(Been a while since I have done C. This is Java (which is 95% the same as C), so might need to be adjusted slightly for C syntax.)



If you have a huge array, you could use a "while" loop with the condition that while count is not equal to a[count], it continues to loop. As soon as it finds a place where they are the same and changes the value, it exits the while loop. That way if you have 30,000 numbers in the array, and the one you need to change is element 10, you do not process 29,990 pointless loops like with a for loop.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...