C/Data Type/Array Int

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Allocate memory for int array

#include <stdio.h>
#include <malloc.h>
main() {
    int  *base; 
    int i;    
    int j;
    int cnt=0;
    int sum=0;
    
    printf("how many integers you have to store \n");  
    scanf("%d",&cnt); 
    base = (int *)malloc(cnt * sizeof(int)); 
    printf("the base of allocation is %16lu \n",base);
    
    if(!base) 
        printf("unable to allocate size \n");
    else {    
      for(j = 0; j < cnt; j++)
          *(base+j)=5;
    }
    sum = 0;
     
    for(j = 0; j < cnt; j++)
      sum = sum + *(base+j);
    printf("total sum is %d\n",sum);
    
    free(base);
    
    printf("addr of allocation is %16lu \n",base);
    base = (int *)malloc(cnt * sizeof(int));
    printf("the base of allocation is %16lu \n",base);
    base = (int *)malloc(cnt * sizeof(int));
    printf("the base of allocation is %16lu \n",base);
    base = (int *)calloc(10,2);  
    printf("the base of allocation is %16lu \n",base);
}


An example of how to initialize a matrix: loop

  
#define X_SIZE 60
#define Y_SIZE 30

int main()
{
    int matrix[X_SIZE][Y_SIZE];
    int x,y;    /* current element to zero */
    for (x = 0; x < X_SIZE; ++x) {
        for (y = 0; y < Y_SIZE; ++y) {
            matrix[x][y] = -1;
        }
    }
    return (0);
}


an example of using an array for two tasks

#include <stdio.h>
int main() {
    int temps[31];
    int i, total;
    float average,celsius;
    total=0.0;
    for( i = 0; i < 31; i++) {
        printf("enter temperature #%d:", i);
        scanf("%d", &temps[i]);
    }
    for(i = 0; i < 31; i++)
        total += temps[ i ];
    
    average = total / 31.0;
    printf("average is:%f\n\n", average);
    puts("fahrenheit \t celsius\n");
    for( i = 0; i < 31; i++) {
        celsius = (5.0 / 9.0) * ( temps[ i ] - 32);
        printf("%d\t\t%6.2f\n", temps[ i ], celsius);
    }
}


Array: assign and retrieve value

  
#include <stdio.h>
int main() {
    int i = 0;
    int a[5];
    
    for(i = 0;i<5;i++) {
        a[i]=i;
    }
    for( i = 0;i<5;i++) {
        printf(" %d\n",a[i]);
    }
}


Array initialization

#include <stdio.h>
int array[] = {3, 2, 1, 3, 6, 4, 2, 2, 9, 3};
int index;
int main()
{
    index = 0;
    while (array[index] != 0)
        ++index;
    printf("Number of elements before zero %d\n",
                  index);
    return (0);
}


Assign int value reference to int array

#include <stdio.h>
void printarr(int *a[]);
void printarr_usingptr(int *a[]);
int *a[5]; 
main() {
    int i1=4,i2=3,i3=2,i4=1,i5=0; 
    int i;
    int j;
    a[0]=&i1; 
    a[1]=&i2;          
    a[2]=&i3;
    a[3]=&i4;
    a[4]=&i5;
    printarr(a);
    printarr_usingptr(a);
}
void printarr(int *a[]) {
     int j;
     printf("Address in array Value\n");
     for(j = 0;j < 5;j++) {
         printf("%16u       %16u           %d\n",a[j],a[j],a[j]); 
     }
}
void printarr_usingptr(int *a[]) {
    int j = 0;
    printf("using pointer\n");
    for( j=0;j<5;j++) {
        printf("value of elements   %d %16lu %16lu\n",**a,*a,a);
        a++;
    }
}


Declares a 100-integer array

#include <stdio.h>
int main(void)
{
  int x[100]; 
  int i;
  /* load x with values 0 through 99 */
  for(i = 0; i < 100; ++i) 
      x[ i ] = i;
  /* display contents of x */
  for(i = 0; i < 100; ++i) 
      printf("%d ", x[ i ]);
  return 0;
}


Define array and use it

#include <stdio.h>
void main()
{
  int numbers[10];                               /* Array storing 10 values     */
  int count = 10;                                /* Number of values to be read */
  long sum = 0L;                                 /* Sum of the numbers          */
  float average = 0.0f;                          /* Average of the numbers      */
  int i = 0;                                     /* Loop counter                */
  printf("\nEnter the 10 numbers:\n");           
  /* Read the ten numbers to be averaged */
  for(i = 0; i < count; i ++)
  {
    printf("%2d : ",i+1);
    scanf("%d", &numbers[i]);                    /* Read a number               */
    sum += numbers[i];                           /* Add it to sum               */
  }
  average = (float)sum/count;                     /* Calculate the average      */
  printf("Average of the ten numbers entered is: %f\n", average);
}


Define int array and assign value

  
#include <stdio.h>
int main(void)
{
  int a1[10], a2[10];
  int i;
  for(i = 1; i < 11; i++) 
      a1[ i - 1 ] = i;
  for( i = 0; i < 10; i++) 
      a2[ i ] = a1[ i ];
  for( i = 0; i < 10; i++) 
      printf("%d ", a2[ i ]);
  return 0;
}


how to use arrays with functions

  
#include <iostream>
using namespace std;
#define iSIZE 5
void vadd_1(int iarray[]);
main( )
{
 int iarray[iSIZE]={0,1,2,3,4};
 int i;
 cout << "iarray before calling add_1:\n\n";
 for(i=0; i < iSIZE; i++)
   cout << "  " << iarray[i];
 vadd_1(iarray);
 cout << "\n\niarray after calling vadd_1:\n\n";
 for(i=0; i < iSIZE; i++)
   cout << "  " << iarray[i];
 return(0);
}
void vadd_1(int iarray[])
{
 int i;
 for(i=0; i < iSIZE; i++)
   iarray[i]++;
}


Int array and its pointer

  
#include <stdio.h>
int main(void)
{
  int a[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
  int *p;
  p = a; /* assign p the address of start of array a */
  /* output a"s first, second and third elements using pointer */
  printf("%d %d %d\n", *p, *(p+1), *(p+2));
  /* this does the same thing using a */
  printf("%d %d %d", a[0], a[1], a[2]);
  return 0;
}


Output array address

  
#include <stdio.h>
void print_addr(int a[]);
main() {
    int a[5];
    int i;
    
    for(i = 0;i<5;i++) {
        a[i]=i;
    }
    print_addr(a);
}

void print_addr(int a[]) {
    int *b;
    int i;
    b = a;
    
    for(i = 0;i < 5; i++) {
        printf("value %d and address is %16lu\n",*b,b);
        b++;
    }
}


Prints out numbers and their doubles

int main()
{
    int data[10];
    int twice[10];
    int index;
    for (index = 0; index < 10; ++index) {
        data[index] = index;
        twice[index] = index * 2;
    }
    return (0);
}


Reference element in array

#include <stdio.h>
char s[80];
int main(void)
{
  s[3] = "X";
  printf("%c", s[3]);
  return 0;
}


Shows two ways an array can be cleared

#define MAX 10
void init_array_1(int data[]) {
    int  index;
    for (index = 0; index < MAX; ++index)
        data[index] = 0;
}
void init_array_2(int *data_ptr) {
    int index;
    for (index = 0; index < MAX; ++index)
        *(data_ptr + index) = 0;
}
int main()
{
    int  array[MAX];
    init_array_1(array);
    init_array_1(&array[0]);
    init_array_2(array);
    return (0);
}


Use for loop to calculate the square and save it to array

  
#include <stdio.h>
int main(void)
{
  int sqrs[ 10 ];
  int i;
  for(i = 1; i < 11; i++) 
      sqrs[ i - 1 ] = i * i ;
  for(i = 0; i < 10; i++) 
      printf( "%d ", sqrs[ i ] );
  return 0;
}