ch3-SimpleArrays (Array initializations in C/C++)

Chapter_3     Exercise_3-17 Exercise_3-18







SimpleArrays     TCP1, p. 197  (simplearrays.c,  SimpleArrays.cpp)




simplearrays.c         download


#include <stdio.h> // for printf(), putchar()

int main()
{
int i;

int a[5] = {}; // initialize elements to 0
for (i = 0; i < 5; i++)
{printf("a[%d] = %d, ", i, a[i]);}
putchar('\n');

int b[5] = {0}; // initialize elements to 0
for (i = 0; i < 5; i++)
{printf("b[%d] = %d, ", i, b[i]);}
putchar('\n');

int c[5] = {0,5}; // initialize second element to 5, the rest to 0
for (i = 0; i < 5; i++)
{printf("c[%d] = %d, ", i, c[i]);} // 0,5,0,0,0
putchar('\n');

int d[5] = {0,1,2,3,4,5}; // warning: 6 initializers for 5 elements
for (i = 0; i < 5; i++)
{printf("d[%d] = %d, ", i, d[i]);} // 0,1,2,3,4
putchar('\n');

int e[5]; // uninitialized elements
for (i = 0; i < 5; i++)
{printf("e[%d] = %d, ", i, e[i]);} // garbage values
putchar('\n');

for (i = 0; i < 5; i++)
{
e[i] = i * 10; // initialize elements
printf("e[%d] = %d, ", i, e[i]); // 0,10,20,30,40
}
putchar('\n');

return 0;
}
/*
gcc simplearrays.c -o simplearrays
./simplearrays
a[0] = 0, a[1] = 0, a[2] = 0, a[3] = 0, a[4] = 0,
b[0] = 0, b[1] = 0, b[2] = 0, b[3] = 0, b[4] = 0,
c[0] = 0, c[1] = 5, c[2] = 0, c[3] = 0, c[4] = 0,
d[0] = 0, d[1] = 1, d[2] = 2, d[3] = 3, d[4] = 4,
e[0] = 0, e[1] = 0, e[2] = -1905635168, e[3] = 22013, e[4] = 774351232,
e[0] = 0, e[1] = 10, e[2] = 20, e[3] = 30, e[4] = 40,
*/











SimpleArrays.cpp         download


#include <iostream>
using std::cout;
using std::endl;

int main()
{
int a[5] = {}; // initialize elements to 0
for (int i = 0; i < 5; i++)
{cout << "a[" << i << "] = " << a[i] << ", ";}
cout << endl;

int b[5] = {0}; // initialize elements to 0
for (int i = 0; i < 5; i++)
{cout << "b[" << i << "] = " << b[i] << ", ";}
cout << endl;

int c[5] = {0,5}; // initialize second element to 5, the rest to 0
for (int i = 0; i < 5; i++)
{cout << "c[" << i << "] = " << c[i] << ", ";} // 0,5,0,0,0
cout << endl;

// int d[5] = {0,1,2,3,4,5}; // error: 6 initializers for 5 elements
int d[5] = {0,1,2,3,4};
for (int i = 0; i < 5; i++)
{cout << "d[" << i << "] = " << d[i] << ", ";} // 0,1,2,3,4
cout << endl;

int e[5]; // uninitialized elements
for (int i = 0; i < 5; i++)
{cout << "e[" << i << "] = " << e[i] << ", ";} // garbage values
cout << endl;

for (int i = 0; i < 5; i++)
{
e[i] = i * 10; // initialize elements
cout << "e[" << i << "] = " << e[i] << ", "; // 0,10,20,30,40
}
cout << endl;

return 0;
}
/*
g++ SimpleArrays.cpp -o SimpleArrays
./SimpleArrays
a[0] = 0, a[1] = 0, a[2] = 0, a[3] = 0, a[4] = 0,
b[0] = 0, b[1] = 0, b[2] = 0, b[3] = 0, b[4] = 0,
c[0] = 0, c[1] = 5, c[2] = 0, c[3] = 0, c[4] = 0,
d[0] = 0, d[1] = 1, d[2] = 2, d[3] = 3, d[4] = 4,
e[0] = 0, e[1] = 0, e[2] = 1863696640, e[3] = 21989, e[4] = -83429328,
e[0] = 0, e[1] = 10, e[2] = 20, e[3] = 30, e[4] = 40,
*/









Chapter_3     Exercise_3-17 BACK_TO_TOP Exercise_3-18



Comments

Popular posts from this blog

Contents