Explore the Manipulated Contents of an Array with Multiplication and Addition

  • Context: MHB 
  • Thread starter Thread starter Henry R
  • Start date Start date
  • Tags Tags
    Array
Click For Summary
SUMMARY

The discussion focuses on the manipulation of an integer array in C, specifically the calculations performed on its elements. The final contents of the array after the operations are: arr[0] = 2, arr[1] = 3, arr[2] = 6, arr[3] = 1, and arr[4] = 1. The calculations involve addition, multiplication, and integer division, with the integer part of the division being taken into account due to the array's integer type. Participants clarified the steps leading to these results, emphasizing the importance of understanding integer arithmetic in programming.

PREREQUISITES
  • Understanding of C programming syntax and data types
  • Knowledge of basic arithmetic operations (addition, multiplication, division)
  • Familiarity with integer division and its implications
  • Ability to trace variable assignments and expressions in code
NEXT STEPS
  • Study C programming arrays and their manipulation techniques
  • Learn about integer division and how it differs from floating-point division in C
  • Explore debugging techniques for tracing variable values in C code
  • Practice writing and executing C programs that involve complex array manipulations
USEFUL FOR

Programmers, computer science students, and anyone interested in understanding array manipulation and arithmetic operations in C programming.

Henry R
Messages
25
Reaction score
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: 103
Technology news on Phys.org
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)
 
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: 105
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?
 
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.
 
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)
 
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.
 
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)
 
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
3
Views
2K