Update Array Elements from User Input | How-To Guide for Homework Assignments

  • Thread starter charmedbeauty
  • Start date
  • Tags
    Array
In summary, To update the element at a specific position in an array based on user input, you would need to convert the input into array indices and then use the assignment operator (=) to assign the desired value to that element. After updating the array, you can reprint it to show the updated values.
  • #1
charmedbeauty
271
0

Homework Statement



I have a simple 8X8 array with initial conditions.

how would I go about updating the an element of this array from user input?

Homework Equations





The Attempt at a Solution



Code:
for(i=0;i<MAX_SIZE;i++){
 24        putchar(i+'1');
 25        putchar('|');
 26 
 27        for(j=0;j<MAX_SIZE;j++){
 28 
 29          if((i==0 && j==0) || (i==0 && j==7) || (i==7 && j==0) || (i==7 && j==7)){
 30             printf("X");
 31             array[i][j] = X;
 32           }
 33             else if((i==3 && j==3) || (i==4 &&j==4)){
 34                printf("w");
 35           }  else if((i==3 && j==4) || (i==4 && j==3)){
 36                 printf("b");
 37           }  else {
 38             array[i][j]= '.';
 39             printf(".");
 40          }

my initial array


however now I have a user input a number and letter

ie input user : "b3"

i have read input using



char letter = getchar()
int number = getchar()
now how would I get the element b3 ([1][2]) to swap from '.' to 'b'

and then reprint the updated array?

I tried this

Code:
             i=letter;
 80        j=number;
 81 
 82        array[i][j] = 'b';
 83 
 84        printf("b");

obviously this just prints 'b' though.
 
Physics news on Phys.org
  • #2
To update the element at position [1][2], you would need to use the assignment operator (=) to assign the value 'b' to the element at that position in the array. So your code could look something like this:

// declare variables for user input
char letter;
int number;

// get user input
printf("Please enter a letter: ");
letter = getchar(); // assuming the user enters 'b'
printf("Please enter a number: ");
number = getchar(); // assuming the user enters '3'

// convert letter to array index
int i = letter - 'a'; // this will give you the index 1 (since 'a' is 97 in ASCII and 'b' is 98, so 98-97=1)

// convert number to array index
int j = number - 1; // this will give you the index 2 (since the user entered '3', which is 51 in ASCII and 51-48=3, so 3-1=2)

// update the element at position [1][2]
array[j] = 'b';

// reprint the updated array
for(i=0;i<MAX_SIZE;i++){
putchar(i+'1');
putchar('|');
for(j=0;j<MAX_SIZE;j++){
printf("%c", array[j]); // use %c to print the character at that position in the array
}
printf("\n");
}
 

Related to Update Array Elements from User Input | How-To Guide for Homework Assignments

1. How do I update an element in an array using user input?

To update an element in an array using user input, you can use a combination of the prompt() function to get input from the user and the splice() method to replace the element in the array with the new value. First, use the prompt() function to get the index of the element you want to update. Then, use the splice() method to replace the element at that index with the new value entered by the user.

2. Can I update multiple elements in an array using user input?

Yes, you can update multiple elements in an array using user input by using a loop to iterate through each element and update them one by one. Alternatively, you can also use the map() method to create a new array with the updated values.

3. Is there a way to update an element in an array without using the index?

Yes, you can use the findIndex() method to find the index of the element you want to update based on a condition or value. Once you have the index, you can use the splice() method to replace the element with the new value.

4. What happens if I try to update an element at an index that does not exist?

If you try to update an element at an index that does not exist in the array, the splice() method will not make any changes to the array. It is important to check that the index entered by the user is within the bounds of the array before attempting to update the element.

5. Can I update elements in a multi-dimensional array using user input?

Yes, you can update elements in a multi-dimensional array using user input by accessing the element using its index in each dimension and using the splice() method to replace the value at that index. Alternatively, you can also use a loop to iterate through each element and update them one by one.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
2
Replies
43
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top