Array
Consecutive group of memory locations
Same name and type
To refer to an element, specify
Array name and position number
Format: arrayname[ position number ]
First element at position 0
n element array c:
c[ 0 ], c[ 1 ]…c[ n - 1 ]
Array elements are like normal variables
c[ 0 ] = 3;
cout << c[ 0 ];
Performing operations in subscript. If x =
3,
c[ 5 – 2 ] == c[ 3 ] == c[ x ]
Initializers
int n[ 5 ] = { 1, 2, 3, 4, 5 };
If not enough initializers, rightmost elements
become 0
If too many initializers, a syntax error is generated
int n[ 5 ] = { 0 }
Sets all the elements to 0
If size omitted, the initializers determine it
int n[] = { 1, 2, 3, 4, 5 };
5 initializers, therefore n is a 5 element array
A Structure is a collection of related data items,
possibly of different types.
A structure type in C++ is called struct.
A struct is heterogeneous in that it can be
composed of data of different types.
In contrast, array is homogeneous since it can
contain only data of the same type.
5
Example:
struct StudentInfo{
int Id;
int age;
char Gender;
double CGA;
};
Example:
struct StudentGrade{
char Name[15];
char Course[9];
int Lab[5];
int Homework[3];
int Exam[2];
};
6
The “StudentGrade”
structure has 5
members of
different array types.
The “StudentInfo”
structure has 4 members
of different types.
A sequence of characters is often referred to as a
character “string”.
A string is stored in an array of type char ending
with the null character '0 '.
char A_string[80];
cout << "Enter some words in a string:n";
//80 is the size of A_string
cin.getline(A_string, 80);
cout << A_string << “nEND OF OUTPUTn";
Output:
Enter some words in a string:
This is a test.
This is a test.
END OF OUTPUT
// string prototype, already included in string.h
//returns length of string(not counting'0‘)
//you don't need to include it in your program
int strlen(const char[]);
int string_length = strlen("abcde");
//string_length is set to 5.
int strcmp(char s1[], char s2[]);
/*compares strings s1 and s2, returns
< 0 if s1 < s2
= 0 if s1 == s2 (i.e. strcmp returns false)
> 0 if s1 > s2
*/
int strncmp(char s1[], char s2[], int limit);
/* Same as strcmp except that at most limit characters are
compared. */
fp
contains all information about file
Communication link between system and program
Mode can be
r open file for reading only
w open file for writing only
a open file for appending (adding) data
FILE *fp; /*variable fp is pointer to type FILE*/
fp = fopen(“filename”, “mode”);
/*opens file with name filename , assigns identifier to fp */
Writing mode
if file already exists then contents are deleted,
else new file with specified name created
Appending mode
if file already exists then file opened with contents safe
else new file created
Reading mode
if file already exists then opened with contents safe
else error occurs.
FILE *p1, *p2;
p1 = fopen(“data”,”r”);
p2= fopen(“results”, w”);
r+ open to beginning for both reading/writing
w+ same as w except both for reading and
writing
a+ same as ‘a’ except both for reading and
writing
pointer can be reused after closing
Syntax: fclose(file_pointer);
Example:
FILE *p1, *p2;
p1 = fopen(“INPUT.txt”, “r”);
p2 =fopen(“OUTPUT.txt”, “w”);
……..
……..
fclose(p1);
fclose(p2);
C provides several different functions for
reading/writing
getc() – read a character
putc() – write a character
fprintf() – write set of data values
fscanf() – read set of data values
getw() – read integer
putw() – write integer
similar to scanf() and printf()
in addition provide file-pointer
given the following
file-pointer f1 (points to file opened in write mode)
file-pointer f2 (points to file opened in read mode)
integer variable i
float variable f
Example:
fprintf(f1, “%d %fn”, i, f);
fprintf(stdout, “%f n”, f); /*note: stdout refers to screen */
fscanf(f2, “%d %f”, &i, &f);
fscanf returns EOF when end-of-file reached
handle one integer at a time
syntax: putw(i,fp1);
i : an integer variable
fp1 : pointer to file ipened with mode w
syntax: i = getw(fp2);
i : an integer variable
fp2 : pointer to file opened with mode r
file pointer moves by one integer position, data
stored in binary format native to local system
getw() returns end-of-file marker EOF when file
end reached
Typical errors that occur
trying to read beyond end-of-file
trying to use a file that has not been opened
perform operation on file not permitted by ‘fopen’ mode
open file with invalid filename
write to write-protected file
how to jump to a given position (byte number) in a
file without reading all the previous data?
fseek (file-pointer, offset, position);
position: 0 (beginning), 1 (current), 2 (end)
offset: number of locations to move from position
Example: fseek(fp,-m, 1); /* move back by m bytes from
current
position */
fseek(fp,m,0); /* move to (m+1)th byte in file */
fseek(fp, -10, 2); /* what is this? */
ftell(fp) returns current byte position in file
rewind(fp) resets position to start of file
can give input to C program from command line
E.g. > prog.c 10 name1
name2 ….
how to use these arguments?
main ( int argc, char *argv[] )
argc – gives a count of number of arguments
(including program name)
char *argv[] defines an array of pointers to
character (or array of strings)
argv[0] – program name
argv[1] to argv[argc -1] give the other arguments
as strings