The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
Arrays are one of a kind of data structure because arrays defines the way of arranging the data, which allows us to manipulated the data in interesting ways.Array is a collection of data, which is very similar to a matrix but a difference is that array can hold only data of similar datatypes i.e) We could have an array of intergers or an array of characters or an array of floating point. It reduces the programmers task.
data_type arrayname[index];
Here we go through some valid array declaration.
Here we go through some invalid array declaration.
Array is most useful when programmers need to declare multiple variable of same type to store similar data(e.g. marks of 20 students). For example, if a user wish to store marks of 5 students, then he might have to declare 5 different variables to store each student's mark. This scenario will be tough, if the user has to store 10 or more students marks. So, c provides the alternative one named array.
Let's workout a program to demonstrate Arrays in C.
#include <stdio.h> int main() { int s1, s2, s3, s4, s5; printf ("Enter students marks details "); printf ("\ns1 = "); scanf ("%d", &s1); printf ("\ns2 = "); scanf ("%d", &s2); printf ("\ns3 = "); scanf ("%d", &s3); printf ("\ns4 = "); scanf ("%d", &s4); printf ("\ns5 = "); scanf ("%d", &s5); printf ("\n---Students marks details---\n "); printf ("s1 = %d\n", s1); printf ("s2 = %d\n", s2); printf ("s3 = %d\n", s3); printf ("s4 = %d\n", s4); printf ("s5 = %d\n", s5); return 0; }
The above program reads and prints the marks of all the 5 students by declaring 5 variables to hold each student's mark. So, the program uses many statements.
Let's workout a program to demonstrate Arrays in C.
#include <stdio.h> int main() { int s[5], i; printf ("Enter students marks details "); for(i = 0; i < 5; i++) { printf ("\ns%d = ", i + 1); scanf ("%d",&s[i]); } printf ("\n---Students marks details--- "); for(i = 0; i < 5; i++) { printf ("\ns%d = %d ", i + 1, s[i]); } return 0; }
In the above program the 's' array name with subscript of 5. The same output with less number of statements in a program even we have to store 1000 students marks.
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019