Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Programming Fundamentals

Lab 06
Topic Functions and Basic Pointers

 Arrays, Filing and Functions.


o Passing array into a function.
o Importance of array size in functions.
Objective o Difference between size and occupy.
o Passing C-String into a function.
o Discuss why size is not required when we pass C-String to a function.
o Problem solving involving filing, function and array/C-String.

Lab Description:

This lab is basically design to discuss about pointers, pointers and its functions, and 1D array

Passing array into a function:

We can pass an array into a function. As we discussed earlier array name represent the base address of
an array. So we can pass an array by its name along with its size. As we discussed earlier size and occupy
are two different things. Size represent the capacity of array and occupy represent the usage of available
resources. You must use occupy instead of size for generic use.

Prototype:

Calling:

Definition:

1
Passing C-String into a function:

As we discussed above we can pass a c-string using same method as an array. But the difference is
passing size is not required in c-string because of null termination.

Prototype:

Calling:

Definition:

Pointer:

C++ pointers are easy and fun to learn. Some C++ tasks are performed more easily with pointers, and
other C++ tasks, such as dynamic memory allocation, cannot be performed without them. We will
discuss dynamic memory in detail after few labs.

As you know every variable is a memory location and every memory location has its address defined
which can be accessed using ampersand & operator (address of variable operator) which denotes an
address in memory.

2
A pointer however, is a variable that stores the memory address as its value. A pointer variable points to
a data type (like int or string) of the same type, and is created with the * operator. The address of the
variable you’re working with is assigned to the pointer.

1D-Array:

An array is a collection of a fixed number of components (also called elements) all of the same data type
and in contiguous (that is, adjacent) memory space. An array whose components are of type char.

Declaration statement of an array:

char list[10];

Above statement is used for creating an array. But as we discussed above when a memory location is
reserved for this array it holds some values which are not assigned by user. So those values are
considered as garbage value. In order to avoid garbage value, it is a good practice to assign values at the
time of creation of array.

Initialization statement of an array:

Assigning value at the time of declaring of array or variable is called initialization statement. There are
multiple ways of initialize an array.

Initialize an array with null character:

char list[5]={}; OR char arr[5]{};

Full array initialization with different elements:

char list[5]={‘a’,’b’,’c’,’d’,’e’};

Partial array initialization with different elements:

char list[5]={‘a’,’b’};

on remaining indexes null will be assigned as initial value in case of partial array initialization.

Accessing array components:

Generic way of accessing an array is: IdentifierName[index#];

3
char list[10]; //list[0] is use for accessing first element of an array.

Array input:

We can take input from user into an array. We can take input index wise one by one and we can also
take input at specific index number.

Input at specific index:

cin>> list[0]; //taking input on index 0 which is the first element of an array.

Input in whole array:

We use repetition statement(loops) for taking input in whole array. We can take input one by one as
mention above but it is not considered as a good practice.

for(int i=0;i<size;i++)// i is used as index number and size is the number element you want to enter.
{
cin>>list[i]; // taking input at specific index i. After every iteration value of i will be updated.
}
Array output:

We can display the values of array on console as output to user. We can display index wise one by one
and we can also take input at specific index number.

Output of specific index:

cout<< list[0]; //display the value of index 0 which is the first element of an array.

Output of whole array:

We use repetition statement(loops) for display whole array. We can display elements one by one as
mention above but it is not considered as a good practice.

for(int i=0;i<size;i++)// i is used as index number and size is the number element you want to display.
{
cout<<list[i]; // display specific index i. After every iteration value of i will be updated.
}

4
Lab Tasks

1. Write a C program that reads a string from the user and outputs the number
of characters in the string.

2. Write a C program that reads a string from the user and converts all of the
characters to uppercase.

3. Write a C program that reads two strings from the user and outputs whether
or not they are equal.

4. Write a C program that reads a string from the user and outputs whether or
not it is a palindrome (reads the same forwards and backwards).

5. Write a C program that reads a string from the user and outputs the number
of vowels in the string.

6. Write a C program that reads a string from the user and removes all spaces
from the string.
5
7. Write a C program that reads a string from the user and outputs the longest
word in the string.

8. Write a C program that reads a string from the user and outputs the string in
reverse order.

9. Write a function that takes two C-strings as arguments and concatenates


them into a new C-string. The function should return a pointer to the
concatenated string.

Sample Input:

char str1[] = "Hello";

char str2[] = "World";

Sample Output:

"HelloWorld"

10.Write a function that takes a C-string as input and returns the length of the
string.

Sample Input:

char str[] = "Hello World";

Sample Output:

11

6
11.Write a function that takes a C-string as input and returns the number of
vowels in the string.

Sample Input:

char str[] = "Hello World";

Sample Output:

12.Write a program that reads a text file and counts the number of words that
start with a vowel and the number of words that start with a consonant.
Display the results on the screen.

7
13.Write a program that reads a text file containing a list of words, removes all
the words that contain a specific character, and writes the updated list to a
new text file.

14.write sample output Write a program that reads input from a text file in "r"
mode and writes it to an output file in "w" mode.

8
15.write sample output Write a program that reads input from a text file in "r"
mode and appends it to an existing output file in "a" mode.

16.write sample output Write a program that reads input from a text file in "r+"
mode, updates the contents of the file, and saves the changes using "w"
mode.

9
17.write sample output Write a program that reads input from a text file in "r+"
mode, appends new content to the file, and saves the changes using "a"
mode.

18.write sample output Write a program that copies the contents of a text file
using "r" and "w" modes simultaneously.

10
19.Write a program that accepts two arrays of integers and their sizes from the
user. The program should find the common elements in both arrays and store
them in a new array. Finally, the program should print out the common
elements array. Use functions to perform the array processing.

20.write sample output Write a program that accepts a string from the user and
counts the number of vowels and consonants in the string. The program
should print out the number of vowels and consonants. Use functions to
perform the string processing.

11
21.Write a program that accepts an array of integers and its size from the user.
The program should find the largest and smallest elements in the array and
print them out. Use functions to perform the array processing.

22.Write a program that accepts two matrices of integers and their dimensions
from the user. The program should find the sum of the two matrices and
store the result in a new matrix. Finally, the program should print out the
resulting matrix. Use functions to perform the matrix processing.

12
23.Write a program that accepts a sentence from the user and reverses each
word in the sentence. For example, if the user inputs "hello world", the
program should output "olleh dlrow". Use functions to perform the string
processing.

24.Write a program that reads data from a text file containing a list of numbers
and calculates the average, maximum, and minimum of those numbers using
functions. The program should output the results to a separate text file.

25.Write a program that reads data from a text file containing a list of strings
and checks if a given string exists in the file using functions. The program
should output either "Yes" or "No" to the console.

26.Write a program that reads data from a text file containing a list of numbers
and checks if the numbers are in ascending order using functions. The
program should output either "Yes" or "No" to the console.

13
27.Write a program that reads data from a text file containing information about
books (title, author, year) and searches for a book by title using functions.
The program should output either "Found" or "Not Found" to the console.

28.Write a program that reads data from a text file containing information about
products (name, price, quantity) and calculates the total value of all products
using functions. The program should output the result to the console.

29.Write a program that reads data from a text file containing a list of names
and counts the number of names that start with a given letter using functions.
The program should output the result to the console.

30.Write a program that reads data from a text file containing information about
cars (make, model, year) and searches for a car by make and model using
functions. The program should output either "Found" or "Not Found" to the
console.

14

You might also like