Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

7. Is there a difference between an individual element of an array and a scalar

ID: 3741588 • Letter: 7

Question

7. Is there a difference between an individual element of an array and a scalar variable of the same type? If so, what is the difference? can you tell whether a particul (a) a named constant? (b) .. a scalar variable? (c) an array variable? 8. How ar declaration statement declares... 9. TRACE the following programs. If a value is garbage (undefined), mark it G. If you aren't confident of an answer, type in, compile and run the program to test it. include int mainO main int b, c, d; print f("b- printf ( "c = printf return 0 main / %d ", %d ", b); c); include int main main int a0, al, a2; al = 7; /* a0 =-, al =-, a2 =-*/ print f ( "a0- print f("al- print f("a2- return 0 %d ", %d ", %d ", a0); al); a2); /* main */

Explanation / Answer

NOTE: I have completed program for your assignment. Please check and let me know if you have any questions. I will acknowledge back with a response within 24 hours. Thanks for your patience.

7) Ans:

1) A variable is a scalar element holding a value stored in memory location whereas an array is collection of elements of the same type.

2) There is no absolutely difference between variable and individual element of the array because both are considered as variables but the way they are referred is different.

3) For example,

int a; /* declares variable a of type integer */

int a[10]; /* declares array variable whose all of its 10 elements are of type integer */

If we want to refer the variable a we can say

printf(" %d", a);

whereas if we want to refer an individual array element then we have to refer through its index. Like second element of an array can be referred as a[1](array element starts from 0 in 'C').

4) In terms of memory allocation each variable has a designated memory address whereas for individual array elements, memory addresses are always referred by taking first location of the array as reference.

8a)

Ans:

constants are usually declared by using const keyword, # define directive and enum. The most preferred way of declaring named constants are using const keyword and #define directive.

The constant variable tells that the variable cannot be changed once it is assigned a value. It is like a read-only variable.

a) The const keyword is used to declare a constant

int const variable = 1;

const int variable =2;

We can declare the const before or after the type. In general it is used to initialize the constant while declaring variable.

b)

The preprocessor #define is another flexible way of defining constants in a program.

#define TRUE 1

#define FALSE 0

Here TRUE and FALSE are declared as constants using #define directive.

8b)

Ans:

A scalar variable is an identifier declared for a particular type. It cannot hold more than one memory element.

int a;

float b;

char c;

Above are some of the examples of scalar variables. The scalar variable can even hold a structure or union.

8c)

Ans:

An array variable is a collection of elements of a common type. Array elements are declared as follows.

int a[100];

where int indicates the type of elements of array a and size of the array is indicated using square brackets. Here in this case it says array a of type int holding 100 memory elements each of which can accomodate an integer value.

Similarly arrays can be used to hold structures of union.

9a)

Ans:

b = 6

c = 7

d = 8

Explanation:

1) The program declares variables b, c and d and initializes it with values 6, 7, 8.

2) Finally the values of b, c and d are printed

9b)

Ans:

a0 = 6

a1 = 7

a2 = 8

Explanation:

1) The program declares variables a0, a1 and a2 of type integer

2) The program finally prints the values of variables a0, a1 and a2

9c)

Ans:

a[0] = 6

a[1] = 7

a[2] = 8

Explanation:

1) The program declares array variable a of size 3 which means the variable can hold three array elements

2) The program then prints the value of array variables a[0], a[1] and a[2]

9d)

Ans:

a[0] = 6

a[1] = 7

a[2] = 8

Explanation:

1) The program declares array variable a of size 3 which means the variable can hold three array elements

2) For each of the array elements integers 0, 1 and 2 are added. a[0] is assigned 6 + 0 element i.e. 6, a[1] is assigned 6 + 1 element i.e. 7 and a[2] is assigned 6 + 2 element i.e. 8

3) The program then prints the value of array variables a[0], a[1] and a[2]

9e)

Ans:

i = 3

a[0] = 6

a[1] = 7

a[2] = 8

Explanation:

1) The program declares array variable a of size 3 which means the variable can hold three array elements

2) The program uses variable i to add to each individual array element. That is a[0] is assigned 6 + i when i is 0 which means a[0] set to 6. i value is incremented to 1 and 6 + i assigned to a[1] which means a[1] set to 7. Then i value incremented again to 2 and 6 + i assigned to a[2] which means a[2] set to 8.

3) Finally the program prints the value of array variables a[0], a[1] and a[2] and i.

9f)

Ans:

k = 3

a[0] = 6

a[1] = 7

a[2] = 8

Explanation:

1) The program declares array variable a of size 3 which means the variable can hold three array elements

2) The program then uses for loop to set values to each individual array elements.

3) Finally the program prints the value of array variables a[0], a[1] and a[2] and k.

9g)

Ans:

n = 3

a[0] = 6

a[1] = 7

a[2] = 8

Explanation:

1) The program declares array variable a of size 3 which means the variable can hold three array elements

2) The program then uses for loop to set values to each individual array elements.

3) Finally the program prints the value of array variables a[0], a[1] and a[2] using for loop.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote