


































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Covers Basics of Array in C Programming in dept
Typology: Slides
1 / 42
This page cannot be seen from the preview
Don't miss anything!



































1/
An array is a collection of elements of the same type that are referenced by a common name.
Compared to the basic data type (int, float) it is an aggregate or derived data type.
All the elements of an array occupy a set of contiguous memory locations.
Why need to use array type?
Consider the following issue:
"We have a list of 1000 students' marks of an integer type. If using the basic data type (int), we will declare something like the following…"
int studMark0, studMark1, ...studMark
By using an array, we just declare like this,
int studMark[1000];
This will reserve 1000 contiguous memory locations for storing the students’ marks. Graphically, this can be depicted as in the following figure.
This absolutely has simplified our declaration of the variables.
We can use index or subscript to identify each element or location in the memory.
Hence, if we have an index of j, studMark[j] would refer to the jth element in the array of studMark.
For example, studMark[0] will refer to the first element of the array.
Thus by changing the value of j, we could refer to any element in the array.
So, array has simplified our declaration and of course, manipulation of the data.
For example, to declare an array of 30 characters, that construct a people name, we could declare,
char cName[30];
Which can be depicted as follows,
In this statement, the array character can store up to 30 characters with the first character occupying location cName[0] and the last character occupying cName[29].
Note that the index runs from 0 to
So, take note the difference between the array size and subscript/index terms.
Examples of the one-dimensional array declarations,
int xNum[20], yNum[50]; float fPrice[10], fYield; char chLetter[70]; The first example declares two arrays named xNum and yNum of type int. Array xNum can store up to 20 integer numbers while yNum can store up to 50 numbers.
The second line declares the array fPrice of type float. It can store up to 10 floating-point values.
fYield is basic variable which shows array type can be declared together with basic type provided the type is similar.
The third line declares the array chLetter of type char. It can store a string up to 69 characters.
Why 69 instead of 70? Remember, a string has a null terminating character (\0) at the end, so we must reserve for it.
Initialization of an array of type char for holding strings may take the following form, char array_name[size] = "string_lateral_constant";
For example, the array chVowel in the previous example could have been written more compactly as follows, char chVowel[6] = "aeiou";
When the value assigned to a character array is a string (which must be enclosed in double quotes), the compiler automatically supplies the NULL character but we still have to reserve one extra place for the NULL. For unsized array (variable sized), we can declare as follow, char chName[ ] = "Mr. Dracula";
C compiler automatically creates an array which is big enough to hold all the initializer.
If you want to retrieve specific element then then you have to specify not only the array or variable name but also the index number of interest.
For example:
int Arr[]={1,3,5,6,8};
printf(“%d\t%d\n”,Arr[1],Arr[2]);
Output: 3 5
Summarize the response of a survey.
Input: Response of the survey, can be in the range between 0 and 10. Assume the population size to be 40.
Output: Frequency of each response.
#include #define SIZE 40 #define ANS 11
int main(void) { int response[SIZE]; int freq[ANS] = {0}; int i; for(i=0; i< SIZE; i++){ scanf(“%d”,&response[i]); ++freq[response[i]]; }
for(i=0;i int rand(void): returns a pseudo-random number in the range of 0 to RAND_MAX.
RAND_MAX: is a constant whose default value may vary between implementations but it is granted to be at least 32767.
Issue: If we generate a sequence of random number with rand() function, it will create the same sequence again and again every time program runs.
The srand() function sets the starting point for producing a series of pseudo-random integers. If srand() is not called, the rand() seed is set as if srand(1) were called at program start.
The pseudo-random number generator should only be seeded once, before any calls to rand(), and the start of the program.
Standard practice is to use the result of a call to srand(time(0)) as the seed. However, time() returns a time_t value which vary everytime and hence the pseudo-random number vary for every program call.
[sourav@gaya]$ ./a.out
1 came out for 113 times
2 came out for 114 times
3 came out for 102 times
4 came out for 86 times
5 came out for 99 times
6 came out for 86 times
Store marks obtained by students in an array. Find if there is more than one student who scored same marks. Assume minimum marks obtained is 30 and maximum marks obtained is 85.