Arrays Flashcards
Arrays
Arrays are collection of similar datatype stored in a contiguous memory locations
syntax for Arrays
int marks[]={1,2,3}
or
int marks[3];
scanf(“%d”,&marks[0]};
printf(“marks at 0 is %d”,marks[0]};
array mask of size three with size of 4byte each(each integer) (2 byte is for older computers),size depends on your system
say mem location be 4096,4100,4104
total memory consumed =3*4 bytes=12 bytes
add GST of 18% to prices in list
include <stdio.h></stdio.h>
int main() {
float prices[3];
printf(“enter the prices\n”);
scanf(“%f”,&prices[0]);
scanf(“%f”,&prices[1]);
scanf(“%f”,&prices[2]);
printf("total price 1 is :%f",prices[0]+0.18*(prices[0])); printf("total price 2 is :%f",prices[1]+0.18*(prices[1])); printf("total price 3 is :%f",prices[2]+0.18*(prices[2]));
}
Pointer arithmetic
some operations can be performed on pointers like increment and decrement operations
int age=22;
int *ptr=&age;
ptr++;
pointer(mem address) will increase by the size of 1 datatype ,similarly ptr–
we can also subtract one pointer from another (difference is given in no:of datatypes)
printf(“%u,%u difference =%u”,ptr,_ptr,ptr-_ptr);
we can also compare two pointer (returns 0 or 1)
printf(“comparison =%u\n”,ptr==_ptr);
Array is a pointer itself
int *ptr=&arr[0];
int *ptr=arr;
both of these code are same an array is a pointer itself that points to the first memory location
Traverse an array
include <stdio.h></stdio.h>
int main() {
int aadhar[5];
int *ptr=&aadhar[0];
for (int i=0;i<5;i++){ printf("%d index : ",i); scanf("%d",(ptr+i)); } for (int i=0;i<5;i++){ printf("%d index is : %d\n",i,*(ptr+i)); } return 0;
}
instead of (ptr+i) you can also use &aadhar[i] and aadhar[i]
Array as function argument
include <stdio.h></stdio.h>
function declaration
void printnumbers(int arr[],int n);
void printnumbers(int *arr,int n);
we do not need to specify the size for which a seperate integer is used
function call
printnumbers(arr,n);
void printnumbers(int arr[],int n);
int main() {
int n,arr[]={1,2,3,4,5,6};
printnumbers(arr,6);
}
void printnumbers(int arr[],int n){
for (int i=0;i<n;i++){
printf(“%d\t”,arr[i]);
}
}
where /t stands for 1 tab
Multi dimensional arrays
2D arrays
int arr[m][n]={{1,2},{3,4}};
arr[i][j]=x
arr[i][j]=y
find the number of odds in the array
include <stdio.h></stdio.h>
int countodd(int arr[],int n);
int main() {
int arr[]={1,2,3,4,5,6,7};
printf(“no:of odd numbers is :%d\t”,countodd(arr,7));
}
int countodd(int arr[],int n){
int count=0;
for (int i=0;i<n;i++){
if (arr[i]%2!=0){
count++;
}
}
return count;
}
find the out
int arr[]={1,2,3,4,5}
*(arr+2)
*(arr+5)
arr points to first memory address of arr
arr+2 points to the 3rd mem address
*(arr+2) =data at 3rd mem address
*(arr+5)=mem location not in array therefore 0(or value in that particular address)
Reverse an array
include <stdio.h></stdio.h>
int reverse_arr(int arr[],int n);
void printnumbers(int arr[],int n);
int main() {
int arr[]={1,2,3,4,5};
reverse_arr(arr,5);
printnumbers(arr,5);
}
int reverse_arr(int arr[],int n){
for (int i=0;i<(n/2);i++){ int first=arr[i]; int second=arr[n-1-i]; arr[i]=second; arr[n-1-i]=first; } } void printnumbers(int arr[],int n){ for (int i=0;i<n;i++){ printf("%d\t",arr[i]); } }
when arrays are passed as arguments then call by reference occurs
as arr[] points to the first mem address
store fibonacci of first n numbers in an array
include <stdio.h></stdio.h>
int main() {
int n;
printf(“enter n :\n”);
scanf(“%d”,&n);
int fib[n]; fib[0]=0; fib[1]=1; for (int i=2;i<n;i++){ fib[i]=fib[i-1]+fib[i-2]; printf("%d\t",fib[i]); } printf("\n"); }
multiplication table using 2D array
include <stdio.h></stdio.h>
void storetable(int arr[][10],int m,int n,int number);
int main() {
int tables[2][10];
storetable(tables,0,10,2);
storetable(tables,1,10,3);
return 0;
}
void storetable(int arr[][10],int m,int n,int number){
for (int i=0;i<n;i++){
arr[m][n]=number*(i+1);
printf(“%d\t”,arr[m][n]);
}
printf(“\n”);
}
when you pass in 2D array (arr[][10]) as an argument then the first dimension is treated as a pointer and the second dimension is treated as a array itself therefore we should define the second dimension to avoid error
length of an array
length=sizeof(array)/sizeof(datatype);
totalsize/size of one datatype