Array

Lesson

코딩도장 핵심요약: 1D Array 핵심요약

Example Code

Example 1

C_array1d_example.c

#include <stdio.h>
#include <stdlib.h>

void printVec(double *_vec, int _row);

int main()
{
	// Static Matrix Allocation  1-D array 
	// fixed array size and initial constant values
	double a[4] = { 1, 2, 3, 4 };
	double b[] = { 2, 3, 4, 5 };
	double c[4] = { 0 };

	// Print 1-D array element
	printVec(a, 4);

	system("pause");
	return 0;
}

void printVec(double *_vec, int _row)
{
	for (int i = 0; i<_row; i++)
	  printf("Vector[%d] = %f \n", i, _vec[i]);
	printf("\n");
}

Exercise

Exercise 1

C_array1D_exercise.c

Declare and define the following functions

void addVec(float _src1[], float _src2[], float _dst[], int _vecLength);

void main() 
{
// Exercise 1 ***********************************************
printf(“Exercise 1\n");

float x[4] = {1,2,3,4};
float y[4] = {5,6,7,8};
float out[4] = { 0 };
float out_dotProduct = 0;

int vecLength = 4;

addVec(x, y, out, vecLength);
printf("addVector result (x + y): ");
printVec(out, vecLength);

system("pause");
}

Exercise 2

C_array1d_exercise2.c

Assign array address to pointer ptr.

Print each element of 1D array by using pointer

#include <stdio.h>
int main(){
    int st[5] = { 1,2,3,4,5 };
    int* ptr;
    
    ptr = _______________;     
    for (int i = 0; i < 5; i++) {
        //  print each element by using pointer  e.g.  (ptr)
        
    }
}

Last updated