Build Arrays, 1 Dimensional and 2-Dimensional in the C Programming Language
What Will I Learn?
Learn to Use Arrays for dimensions
- 1 Dimension
- 2 Dimensions
Requirements
Dev C++ 5-11
Difficulty
- Intermediate
Tutorial Contents
THEORY
An array or array is a set of data that has the same name and type. The array is often referred to as an indexed variable. The value of a data in an array is determined by the name and index. Arrays are widely used in operations involving indices such as statistics and matrices. The array data type can be a one-dimensional array, two-dimensional, three-dimensional or multiple dimensions. In general, the array form in C is as follows:
Tipe_data Name_array [size];
For an array of more than 1 dimension, it is described as follows:
Tipe_data Name_array [size_1] [size_2] ... [size_n];
For example,
Char name [50];
The above arrays explain that the array name is a single dimension array with a size of 10 times the amount of char type memory. The initial position of the array counts from position 0 instead of 1, so if the name is declared as above, it means that the initial position of the array name is 0 and the last is 9 (not 10).
REPRESENTATION OF ARRAY
To be able to use the type of array in the program it is necessary to declare the previous example. As for the value filling in the array is done one by one corresponding to the index number for example:
double value [7];
value [0] = 9.5;
value [1] = 16;
value [2] = 8.5;
value [3] = 7.75;
value [4] = 4.45;
value [5] = 3.5;
value [6] = 2.55;
Or by decoding it as an array constant, like the following example:
double value [7] = {9.5,16,8.5,7.75,4.45,3.5,2.55};
Specifically for the type of char as a string can be directly written all the characters in the double quotes as in the following example:
char name [15] = "cuthamza";
1 Dimension Array
Here is an example of coding (program) to input and re-display 1-dimensional array.
int main ()
{
int i, bil_round [5];
float bil_real [5];
char name [20];
/* Enter the array bil_round, bil_odd*/
for(i=0;i<5;i++)
{
printf ("\n Enter integers to %d:", i + 1);
scanf("%d",&bil_round[i]);
printf ("enter fractions to %d:", i + 1);
scanf ("%f", & bil_real [i]);
}
/* Enter the name of the array */
printf ("\n Enter your name: \n"); scanf ("%s", & name);
/* display the contents of an array of bil_round, bil_odd */
for(i=0;i<5;i++)
{
printf ("integers to %d: %d\n", i + 1, bil_round [i]);
printf ("integers to %d: %f\n", i + 1, bil_real [i]);
}
/* display the array name */
printf ("\n you name: \n% s", name);
return 0;
}
See the results
2 Dimensional Array
Here is an example of a program listing which enters and re-displays the 2-dimensional array (matrix). The following program listing is a summation program of two matrices where the elements of the matrix are inputted by the user.
int main()
{
int i,j;
int matrikA[3][3], matrikB[3][3], matrixResult[3][3];
/* inputs element from matrix A */
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n Insert element A[%d][%d]:",i+1,j+1);
scanf("%d",&matrikA[i][j]);
}
}
/* inputs element from matrix B */
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n Enter element B [%d][%d]:",i+1,j+1);
scanf("%d",&matrikB[i][j]);
}
}
/* displays element from matrix A */
printf("\n The elements of a matrix A is: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",matrikA[i][j]);
}
printf("\n");
}
/* displays the elements of the matrix B */
printf("\n The elements of matrix B are: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",matrikB[i][j]);
}
printf("\n");
}
/* Operation Summation 2 matrix */
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
matrixResult[i][j]=matrikA[i][j]+matrikB[i][j];
}
}
/* feature element of Matrix Results Summation */
printf("\n The summation result matrix is: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",matrixResult[i][j]);
}
printf("\n");
}
return 0;
}
See the results
Finish
Thank you
Posted on Utopian.io - Rewarding Open Source Contributors
Hey @cuthamza I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Contribution to open source project, I like you and upvote.
because sharing in this is fun.
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]