0% found this document useful (0 votes)
44 views

L02 Elements of Programming

1. C programming supports various simple data types including char, short, int, long, float, double, and pointers. Matrices can be represented as 2D arrays stored in contiguous memory with the rows arranged one after another. 2. Pointers in C store addresses in memory and can be used to access values at specific memory addresses. Arrays are a type of pointer to a contiguous block of memory where each element is a fixed size. 3. Matrices can be dynamically allocated in C using pointers to pointers, with the inner pointers representing each row stored consecutively in memory. Functions are used to allocate and free the memory for a matrix.

Uploaded by

Okjunwon
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

L02 Elements of Programming

1. C programming supports various simple data types including char, short, int, long, float, double, and pointers. Matrices can be represented as 2D arrays stored in contiguous memory with the rows arranged one after another. 2. Pointers in C store addresses in memory and can be used to access values at specific memory addresses. Arrays are a type of pointer to a contiguous block of memory where each element is a fixed size. 3. Matrices can be dynamically allocated in C using pointers to pointers, with the inner pointers representing each row stored consecutively in memory. Functions are used to allocate and free the memory for a matrix.

Uploaded by

Okjunwon
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Elements of programming

Simple data types in C :

char signed char unsigned char


short int long
unsigned short unsigned unsigned long
float double long double

Operator sizeof is used for defining of number of bytes of specific object

Example:
int i = 2000; float f = 16.4;
int i_bytes = sizeof(i);
int f_bytes = sizeof(f);
// for the most systems, i_bytes is 2, f_bytes is 4

Matrices: overview, properties, samples


Elements of programming
Type conversion
Let the expression x + y has a value and a type. If x and y are of the same type
(for example, int), then x + y is int too.
f

If x and y have different types, then x + y is a mixed expression.


Example:
short x; int y;
// x + y will be int
1. Any char or short will be converted to int. Values, which can not
presented as int, will be converted to unsigned.
2. If after the first step the expression has a mixed type, operand of
the lower type converses to the operand of higher type according to
the type hierarchy (see example):

int < unsigned int < long < unsigned long


< float < double < long double

Matrices: overview, properties, samples


Elements of programming
Example: int < unsigned int < long < unsigned long
< float < double < long double

char c; double d; float f; int i;


long lg; short s; unsigned u;

Mixed Mixed
Type Type
expression expression

c = s/i int ? u * 3 – i ?
unsigned

?
u * 3.0 – i double f * 3 – i float?

c + 1 int ? 3 * s * lg long ?

c + 1.0 ?
double d + s ?
double

Matrices: overview, properties, samples


Elements of programming
Structure of C program

matrix.h matrix_functions.c matrix.c


#include <stdio.h> #include "1st.h" #include "1st.h"
#include <alloc.h>
#include <stdlib.h> Matrix CreateMatrix (int void main (void)
#include <conio.h> Size, int Size1) {
#include <dir.h> { // some data
#include <dos.h> . . . …
} //some actions
typedef double **Matrix; void KillMatrix (Matrix ...
MatrA) ...
Matrix CreateMatrix (...); { }
void KillMatrix (...); free(MatrA);
void PrintMatrix (...); }
void EnterMatrix (...); void PrintMatrix (Matrix
void PrintToFile (...); MatrA, int Size, int
void AddMatrix (...); Size1)
void CreateRandomMatrix (...); {
... ...
}
...

Matrices: overview, properties, samples


Elements of programming
Type conversion
int i = 10;
(double)i;
// operator of type conversion: (type)
// variable i is still int, but the result of the
// expression is double

Priority:

(float) i + 3 is equivalent to: ((float) i) + 3

Matrices: overview, properties, samples


Elements of programming
Example of coding:
// miles to kilometers conversion
#include <stream.h>

double mi_to_km(int miles)


{
const double m_to_k = 1.609; // conversion constant
return (miles * m_to_k);
}
void main (void)
{ С++
int miles;
do {
cout << “Enter distance in miles:”;
cin >> miles;
cout << “\nThis distance -” << mi_to_km(miles) << “km.\n”;
} while (miles != 0);
}

Matrices: overview, properties, samples


Elements of programming
Pointers in C
Pointers are used to refer to variables and addresses in computer memory.
Array in C is a kind of a pointer, connected to the continuous fragment of a memory.
Pointers are used for organizing access to memory.

If v is a variable, then &v is address of v in memory, so


using &v is getting the address of variable v in computer memory.

Example:
int* p; // p is a pointer to int

p = &I; // address of the object i


p = 0; // special preserving value (zero)
// (p “points to nowhere”)

p = (int *) 1507; // absolute address


// in computer memory
Matrices: overview, properties, samples
Elements of programming
Addressing and referring
* – referring
If p is a pointer, then *p is a value of the variable the pointer points to.
Value of p is a place in computer memory.
*p is a value placed in computer memory by the address storing in p.

In some sense, * is inverse for &.


Example:

int i = 5, j;
int* p = &i; // p is address of i

cout << *p << “ = i stored at ” << p << endl;


j = p; // illegal! (conversion of a pointer to int)
j = *p + 1; // legal
p = &j; // p points to j
Matrices: overview, properties, samples
Elements of programming
Array is a group of elements of the same type. An array is stored in computer memory so that the
indices of elements allows easily calculate the address of the corresponding value.

For example, let an array A consists of elements located in the memory as a row in ascending order
of indexes, and each element is in k bytes. Then the address of i-th element is calculated by the
formula:
address(A[i]) = address(A[0]) + i*k

int A[4] = {1, 2, 3, 4};


int *pA = A;

*A 1 ?
*pA 1 ?
*pA + 1 2 ?
pA[3] 4 ?
*(pA+1) 2 ?

Matrices: overview, properties, samples


Elements of programming
char String[10] = "ABCDEFGH"
char *pString = String;

pString “ABCDEFGH” ? char *


*pString ‘A’ ? char
pString[0] ‘A’ ? char
pString[3] ‘D’ ? char
&pString[3] "DEFGH“ ? char*
pString+3 "DEFGH“ ? char*
String[5] ‘F’ ? char
(pString+3)[0] ‘D’ ? char
(pString+5)[2] ‘H’ ? char
&(pString+5)[2] "H” ? char*

Matrices: overview, properties, samples


Elements of programming
How to store a matrix in computer memory?

Arrays:

int a[100]; // 1D-array


int b[3][b]; // 2D-array
int c[7][9][2]; // 3D-array

int a[2][3] = {{1, 2, 3}, {4, 5, 6}};


int b[2][3] = {1, 2, 3, 4, 5, 6};

char name[3][9] = {“laura”, “michelle, “paul”};


// ‘\0 will be added to short values

Matrices: overview, properties, samples


Elements of programming
How to store a matrix in computer memory?

Matrix is a 2D array. matrix

0 1 2 row_size – 1
// Dynamic 2D arrays 0
typedef struct Matrix {
1
double** data;
int row_size; 2
int column_size;
}

// or
typedef double** Matrix;
0 1 2 row_size – 1

column_size – 1

Matrices: overview, properties, samples


Elements of programming
Matrix CreateMatrix (int Size, int Size1)
{
. . .
MatrA = (Matrix)malloc(Size*sizeof(double*)+Size*Size*sizeof(double));
if (MatrA == NULL) { printf("Not enough memory!"); exit(1); }

for (i=0; i<Size; i++) MatrA[i] = (double*)(MatrA+Size1)+i*Size;


return MatrA;
}
matrix
void KillMatrix (Matrix MatrA)
{ 0 1 2 row_size – 1
free(MatrA); 0
}
1

2
0 1 2 row_size – 1

column_size – 1

Matrices: overview, properties, samples


Elements of programming
How to store a matrix in computer memory?
Matrix CreateMatrix (int Size, int Size1)
{
Matrix MatrA;
int i;

MatrA = (Matrix)malloc(Size1*sizeof(double*)
+Size1*Size*sizeof(double));
if (MatrA == NULL) { printf("Not enough memory!"); exit(1); }

for (i=0; i<Size1; i++) MatrA[i] = (double*)(MatrA+Size1)+i*Size;


return MatrA;
}

void KillMatrix (Matrix MatrA)


{
free(MatrA);
}

Matrices: overview, properties, samples

You might also like