#include
<stdio.h>
void
main()
{
char a[11]
= "sweet girl";
int i;
for(i
= 0; i <= 9; i = i + 1)
{
a[i + 1] = a[i];
// printf("i = %d
%s\n", i, a);
}
printf("%s\n",
a);
}
-
When
i was 0, which index
of
a was changed? To which
value?
-
When
i was 1, which index
of
a was changed? To which
value?
-
When
i was 2, which index
of
a was changed? To which
value?
-
When
i was 9, which index
of
a was changed? To which
value?
|

-
Index
1 was changed to 0. a[1]=a[0]. Assign a[0] to a[1].
-
Index
2 was changed to 1. a[2]=a[1]. Assign a[1] to a[2].
-
Index
3 was changed to 2. a[3]=a[2]. Assign a[2] to a[3].
-
Index
10 was changed to 9. a[10]=a[9]. Assign a[9] to a[10].
When the
first printf() uncommented the following is the output.

|
|
|
#include
<stdio.h>
void
main()
{
char a[11]
= "sweet girl";
int i;
for(i
= 0; i <= 9; i = i + 1)
{
a[i] = a[9 - i];
// printf("i = %d
%s\n", i, a);
}
printf("%s\n",
a);
}
-
When
i was 0, which index
of
a was changed? To which
value?
-
When
i was 1, which index
of
a was changed? To which
value?
-
When
i was 2, which index
of
a was changed? To which
value?
-
When
i was 9, which index
of
a was changed? To which
value?
|

-
Index
0 was changed to 9. a[0]=a[9], assign a[9] to a[0].
-
Index
1 was changed to 8. a[1]=a[8], assign a[8] to a[1].
-
Index
2 was changed to 7. a[2]=a[7], assign a[7] to a[2].
-
Index
9 was changed to 0. a[9]=a[0], assign a[0] to a[9].
When the
first printf() uncommented the following is the output.

|
| |
|
#include
<stdio.h>
void
main()
{
int i,
x[6], y[6] = {3, 8, 2, 9, 4, 1};
for(i
= 0; i <= 5; i = i + 1)
x[i] = y[i];
for(i
= 0; i <= 5; i = i + 1)
printf("x[%d] =
%d\t, y[%d] %d\n", i, x[i], i, y[i]);
}
-
What are the names of the arrays?
-
What are the names of the scalars?
Scalars are the variables that are not arrays.
-
How many indexes do each of the
arrays have?
-
What is the lowest index and the
highest index for the arrays?
-
Which array is initialized during
its declaration?
-
Which array is assigned values in
a loop?
-
Are the arrays printed horizontally
or vertically?
-
How would you write the code if
you had wanted to write the arrays the opposite way?
|

-
x
and y.
-
i.
-
6.
-
The
lowest index is 0 and the highest is 5.
-
Array
y.
-
Array
x.
-
Vertically.
-
Change
the for loop components to the following for both arrays.
for(i = 5; i >= 0; i = i - 1)
#include <stdio.h>
void main()
{
int i, x[6],
y[6] = {3, 8, 2, 9, 4, 1};
for(i = 5;
i >= 0; i = i - 1)
x[i] = y[i];
for(i = 5;
i >= 0; i = i - 1)
printf("x[%d] = %d,\t y[%d] %d\n", i, x[i], i, y[i]);
}

|
| |
|
#include
<stdio.h>
void
main()
{
int i,
x[6], y[7] = {3, 8, 2, 9, 4, 1, 0};
for(i
= 0; i <= 5; i = i + 1)
x[i] = y[i + 1];
// Statement 1
for(i
= 0; i <= 5; i = i + 1)
printf("x[%d] =
%d\t y[%d] = %d\n", i, x[i], i, y[i]);
}
-
When
i is 0, which index
of
y is used? Which index
of
x is changed?
-
When
i is 1, which index
of
y is used? Which index
of
x is changed?
-
When
i is 5, which index
of
y is used? Which index
of
x is changed?
-
Can you determine what would be
printed if
Statement
1 were changed to:
x[i]
= y[i] + 1;?
-
Can you determine what would be
printed if
Statement
1 were changed to:
x[i]
= y[6 -i];?
-
Can you determine what would be
printed if
Statement
1 were changed to:
x[i]
= y[1];?
|

-
Index
1 of y was used. No change on index x.
-
Index
2 of y was used. No change on index x.
-
Index
6 of y was used. No change on index x.
-

-

-

|
| |
|
-
When running the following program,
enter the following input:
6,
4.0,
5.0,
2.0,
55.0,
8.0,
1.0.
The first number is the count and the rest are the items that go
in the array. When scanning a float, use
%f
instead of
%.2f.
#include
<stdio.h>
void
main()
{
int count,
i;
float
a[6];
printf("How many
numbers do you have?\n");
scanf_s("%d",
&count);
printf("Enter the
number(s): \n");
for(i
= 0; i < count; i = i + 1)
scanf_s("%f",
&a[i]);
for(i
= 0; i < count; i = i + 1)
printf("a[%d] =
%.2f\n", i, a[i]);
}
-
What was the value of
count?
-
In the
scanf_s()
and the
printf(),
did
i
ever reach the value of
count?
Why or why not?
-
The
4.0
was read into which index of the array?
-
The
5.0
was read into which index of the array?
|

-
6.
-
i
did not reach the value of count because the terminal condition
is i < count and an array index start from 0 instead of 1.
-
Index
0.
-
Index
1.
|
| |
|
#include
<stdio.h>
void
main()
{
int i,
j;
float
a[30];
printf("Enter floats
that are < 10, 10 and above to stop:\n");
scanf_s("%f",
&a[0]);
for(i
= 0; a[i] <= 10.0; i = i + 1)
scanf_s("%f",
&a[i + 1]);
// printf("Last value
of i was %d\n", i);
for(j
= 0; j < i; j = j + 1)
printf("a[%d] =
%.2f\n", j, a[j]);
}
-
Does the
scanf_s()
here change the value of
i?
-
The first data item was read into
which index of the array?
-
The first time the condition
a[i] <= 10.0
was checked, i was 0. Was the condition
true or false? During the first time through the loop, into
which index did the
scanf_s()
place a data item?
-
The second time that the condition
a[i] <= 10.0
was checked, i was 1? Was the condition
true or false? Into which index did the next
scanf_s()
place a new data item during the second iteration of the loop?
-
When the condition
a[i] <= 10.0
finally became false, what was the value of
i?
Run the program again by removing the
//
(uncomment) to check your answer.
-
In the second loop does
j
ever reach this last value of
i
in the
printf()?
|
------------------------------------------------------------------------

-
No.
-
Index
0.
-
True.
Index 0.
-
True.
Index 2.
-
i
was 3.

-
No,
at this stage i = 3 and j = 3.
|
| |
|
More Question and Activities
-
Show the output from each code using
the given array and
for
loop. Remember that the first index is always
0
and not
1.
Use the same
for
loop with each
printf().
Study the source code and the output.
#include
<stdio.h>
void
main()
{
int i, a[
] = {40, 20, 70, 10, 80, 30, 90};
for(i = 1;
i <= 5; i = i + 1)
-----------------------------
printf("\n");
}
Example:
#include
<stdio.h>
void
main()
{
int i, a[
] = {40, 20, 70, 10, 80, 30, 90};
for(i = 1;
i <= 5; i = i + 1)
printf("%d\t",
a[i]);
printf("\n");
}
-
printf("%d\t",
i);
-
printf("%d\t",
a[i + 1]);
-
printf("%d\t",
a[i -1]);
-
printf("%d\t",
a[i] + a[i + 1]);
-
printf("%d\t",
a[i] + 1);
-
printf("%d\t",
a[i] + i);
|
-------------------------------------------------------------------------------------------
-

-

-

-

-

-

|
| |
|
-
Show the contents of the new array after
it is changed by each assignment statement and complete the code.
Use the same
for
loop with each assignment statement. Start with the given array
for each. When doing these, remember that during each iteration
of the loop, array elements are changing. This is especially important
in question 2.d, 2.f and 2.h. Study the source code and the output.
#include
<stdio.h>
void
main()
{
int i, a[
] = {40, 20, 70, 10, 80, 30, 90};
for(i = 1;
i <= 5; i = i + 1)
{
--------------------------------------
--------------------------------------
}
}
Example:
#include
<stdio.h>
void
main()
{
int i, a[
] = {40, 20, 70, 10, 80, 30, 90};
for(i = 1;
i <= 5; i = i + 1)
{
a[i] = 0;
printf("a[%d] = %d ",
i, a[i]);
}
printf("\n");
}
-
a[i] = i;
-
a[i] = a[i + 1];
-
a[i + 1] = a[i];
-
a[i] = a[i] + a[i]
+ a[i + 1];
-
a[i + 1] = a[i]
+ a[i + 1];
-
a[6 – i] = a[5 –
i];
-
a[5 – i] = a[6 –
i];
|

-

-

-

-

-

-

-

|