Professional Documents
Culture Documents
ECE/ECM
Arrays and Multi Dimensional
U n i t - 11
Multidimensional
Arrays
C++ also allows an array to have more than one dimension.
The declaration must specify the number of rows and the number of columns, and
both must be constants.
Processing a 2-D
Array
A one-dimensional array is usually processed via a for loop. Similarly, a two-
dimensional array may be processed with a nested for loop:
Each pass through the inner for loop will initialize all the elements of the current row to 0.
The outer for loop drives the inner loop to process each of the array's rows.
Initializing in
Declarations
int Array1[2][3] = { {1, 2, 3} , {4, 5, 6} };
int Array2[2][3] = { 1, 2, 3, 4, 5 };
int Array3[2][3] = { {1, 2} , {4 } };
2 Dimensional 3 Dimensional
double Coord[100][100][100];
•
2-D Arrays as
Parameters
When passing a two-dimensional array as a parameter, the base address is passed,
as is the case with one-dimensional arrays.
But now the number of columns in the array parameter must be specified. This is
because arrays are stored in row-major order, and the number of columns must be
known in order to calculate the location at which each row begins in memory:
C++ supports several mechanisms for aggregate data types: arrays, structures, classes.
These allow complex combinations of other types as single entities.
C++ also supports other mechanisms that allow programmers to define their own
custom data types: enum types and typedefs.
Enumerate
dAn enumerated
Typestype is defined by giving a name for the type and then giving a list of
labels, which are the only values that a variable of that type is allowed to have.
Enumerated types allow the creation of specialized types that support the use of meaningful
labels within a program. They promote code readability with very little overhead in
memory or runtime cost.
enum Month {JAN, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC};
enum Season {WINTER, SPRING, SUMMER, FALL};
enum Hemisphere {NORTH, SOUTH, EAST, WEST};
Month Mon;
Season Period;
Hemisphere Region;
. . .
if (Mon == JAN && Hemisphere == NORTH)
Period = WINTER;
Enumerated type variables do not contain the character string of the value. The internal
representation uses integer values; it is bad practice to rely on those values.
Since the labels are logically constant values it is common to express them using all
capital letters, just as for named constants.
Enumerated type variables can be passed as parameters and used as the return value of a
function.
Good use of enumerated data types make the program more readable by providing a
way to incorporate meaningful labels (thus easier to debug) and they help in self-
documenting the program.
s im p l e t y p e s
in t e g r a l f lo a t in g
• enum
• a user-defined data type
• formed by listing identifiers
INTRODUCTION
1. COURSE OBJECTIVES:
The objectives of this course are:
1. Provide exposure to problem solving through C programming
2. Explore the structure and syntax of C programming language
3. illustrate the applications of data types, operators, arrays, and control flow
statements in problem solving.
a. Demonstrate the usage of procedure-oriented programming.
b. Provide insight into concepts like pointers, structures, and unions
14
INTRODUCTION
15
WHY NAME 'C' WAS GIVEN TO THIS
LANGUAGE?
Many of the ideas of C language were derived and taken from 'B' language.
BCPL and CPL are previous versions of 'B' language.
As many features came from B it was named as 'C
16
FEATURES OF C
o C is a structured programming language
o C supports functions that enables easy maintainability of code, by
Implementation phase
implement the program in some programming language
18
STEPS IN PROBLEM SOLVING
First produce a general algorithm (one can use pseudo code)
19
PSEUDOCODE & ALGORITHM
o Example 1: Write an algorithm to determine a student’s final grade and indicate whether it is
passing or failing. The final grade is calculated as the average of four marks
Pseudo code:
Input a set of 4 marks
20
DETAILED ALGORITHM
21
THE FLOWCHART
(Dictionary) A schematic representation of a sequence of operations, as in a manufacturing
process or computer program.
22
PSEUDOCODE & ALGORITHM
1.A Flowchart
⚫ shows logic of an algorithm
23
FLOWCHART SYMBOLS
Basic
Name Symbol Use in Flowchart
2. C's Keywords
26
THE KEYWORDS
27
Basic Structure Of “C” Programs
#include<stdio.h>
Header Files
#include<conio.h>
Entry Point Of
Program
void main()
Indicates Starting
{ of Program
-- other statements
}
HEADER FILES
The files that are specified in the include section is called as Header File.
These are precompiled files that has some functions defined in them.
29
MAIN FUNCTION
From main function the flow goes as per the programmers choice.
30
RUNNING A ‘C’ PROGRAM
Type a program.
Save it.
Run the program (Actually the exe created out of compilation will run and not
the .c file)
In different compiler we have different option for compiling and running.
31
RUNNING A ‘C’ PROGRAM
Type a program.
Save it.
Run the program (Actually the exe created out of compilation will run and not
the .c file)
In different compiler we have different option for compiling and running.
32
“C” language TOKENS
The smallest individual units in a C program are known
as tokens. In a C source program, the basic element
recognized by the compiler is the "token." A token is source-
program text that the compiler does not break down into
component elements.
C has 6 different types of tokens viz.
1. Keywords [e.g. float, int, while]
2.Identifiers [e.g. main, amount] 3.
Constants [e.g. -25.6, 100]
4. Strings [e.g. “SMIT”, “year”] Strin
gs