one dimensional array in c

One Dimensional Array



Array

  Group of consecutive memory locations
    A block of many variables of the same type, Same name
    can be declared for any type


Examples
    list of students’ marks
    series of numbers entered by user
    Vectors
    matrices


To refer to an element, specify
    Array name
    Position number


Format
    arrayname[ position number ]
    First element at position 0
    n element array named c:
         c[ 0 ], c[ 1 ]...c[ n – 1 ]

 

Array Example: Taking input From the user and showing Output.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
int main() {
	int numbers[5],i;

	printf("Enter 5 integers:\n");
	// taking input from user
	for(i = 0; i < 5; ++i) {
		scanf("%d", &numbers[i]);
	}

	printf("Display all numbers:\n");

	// printing all numbers
	for(i = 0; i < 5; ++i) {
		printf("%d\n", numbers[i]);
	}
	return 0;  

 } 


Enter 5 integers:
10
21
45
55
45
Display all numbers:
10
21
45
55
45

 

Change value

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
int main() {
  int values[5] ={10,4,-5,8,3};
  int i;
  
  printf("Before changing value:\n");
  for(i = 0; i < 5; ++i) {
     printf("%d\n", values[i]);
  }
  
  values[2] = 100; //Change Value of Array elements

  printf("After changing value:\n");
  
  for(i = 0; i < 5; ++i) {
     printf("%d\n", values[i]);
  }
  return 0;
}

 

Output

Before changing value:
10
4
-5
8
3
After changing value:
10
4
100
8
3
Next Post Previous Post
No Comment
Add Comment
comment url