Explore the Manipulated Contents of an Array with Multiplication and Addition

  • MHB
  • Thread starter Henry R
  • Start date
  • Tags
    Array
In summary, the contents of the array after the given manipulations are as follows: arr[0] = 2, arr[1] = 3, arr[2] = 6, arr[3] = 1, arr[4] = 1. The values in the array were obtained by performing multiplication, addition, and division operations using the given expressions. It is important to note that the array treats all numbers as integers, so any decimal values are rounded down to the nearest integer.
  • #1
Henry R
25
0
Please identify the contents of the array after the following manipulations.

int arr[5];

arr[0] = 2;
arr[1] = arr[0] + 1;
arr[2] = arr[0] * arr[1];
arr[3] = arr[1] / arr[0];
arr[4] = arr[3] * arr[3];

View attachment 3674

*Involves multiplication, addition from the array. Solve it...
 

Attachments

  • array.PNG
    array.PNG
    555 bytes · Views: 54
Technology news on Phys.org
  • #2
Henry R said:
Please identify the contents of the array after the following manipulations.

int arr[5];

arr[0] = 2;
arr[1] = arr[0] + 1;
arr[2] = arr[0] * arr[1];
arr[3] = arr[1] / arr[0];
arr[4] = arr[3] * arr[3];

View attachment 3674

*Involves multiplication, addition from the array. Solve it...

This is easy.. At the beginning there is the command [m]arr[0]=2;[/m], then you just have to replace the value that [m]arr[0][/m] got at the second expression.
So you will have arr[1]=2+1=3.
In the same way you can find the other contents of the array (Whew)
 
  • #3
evinda said:
This is easy.. At the beginning there is the command [m]arr[0]=2;[/m], then you just have to replace the value that [m]arr[0][/m] got at the second expression.
So you will have arr[1]=2+1=3.
In the same way you can find the other contents of the array (Whew)

This is my effort. Just got confuse.

View attachment 3675
 

Attachments

  • array.PNG
    array.PNG
    1.2 KB · Views: 49
  • #4
Henry R said:
This is my effort. Just got confuse.

https://www.physicsforums.com/attachments/3675

It isn't like that... (Shake) How did you find these results?
 
  • #5
evinda said:
It isn't like that... (Shake) How did you find these results?

no, idea. But, I think the second expression is correct according to your explanation.
 
  • #6
Henry R said:
no, idea. But, I think the second expression is correct according to your explanation.

It should be like that:

[m]arr[1]=2+1=3
arr[2]=2*3=6
arr[3]=3/2=1
arr[4]=1*1=1[/m]

Do you understand why? (Thinking)
 
  • #7
evinda said:
It should be like that:

[m]arr[1]=2+1=3
arr[2]=2*3=6
arr[3]=3/2=1
arr[4]=1*1=1[/m]

Do you understand why? (Thinking)

arr[0] = 2
arr[1] = 3

expression 3...
arr[2] = arr[0] * arr[1] =6, is because 2 * 3 = 6.
 
  • #8
Henry R said:
arr[0] = 2
arr[1] = 3

expression 3...
arr[2] = arr[0] * arr[1] =6, is because 2 * 3 = 6.

[m] arr[3]=arr[1]/arr[0]=3/2=1[/m]
We take the integer part of the number $1.5$ since [m]arr[/m] is an array of integers. (Nerd)
 
  • #9
evinda said:
[m] arr[3]=arr[1]/arr[0]=3/2=1[/m]
We take the integer part of the number $1.5$ since [m]arr[/m] is an array of integers. (Nerd)

Oh, Thank you so much for reminds me that. Yeah! It's array treat 1.5 as integer.
 

Related to Explore the Manipulated Contents of an Array with Multiplication and Addition

1. What is an array?

An array is a data structure that can hold multiple values of the same data type in a single variable. It is a linear collection of elements, where each element can be accessed by its index.

2. How do you declare an array?

To declare an array, you use the syntax: data_type[] array_name = new data_type[size]. For example, to declare an array of integers with a size of 5, you would use: int[] numbers = new int[5].

3. How do you access elements in an array?

You can access elements in an array by using their index. The first element in an array has an index of 0, the second element has an index of 1, and so on. You can also use a loop to iterate through all the elements in an array.

4. Can you change the size of an array once it is declared?

No, the size of an array cannot be changed once it is declared. If you need to add or remove elements from an array, you can create a new array with the desired size and copy the elements from the original array to the new one.

5. What are some common operations performed on arrays?

Common operations performed on arrays include adding and removing elements, sorting elements, and searching for specific elements. Arrays are also often used in conjunction with loops to perform operations on all elements in the array.

Similar threads

  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
2K
  • Programming and Computer Science
Replies
17
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top