Purpose of Study Data Structure and Algorithm: Write Optimized and Scalable Code
Purpose of Study Data Structure and Algorithm: Write Optimized and Scalable Code
Purpose of Study Data Structure and Algorithm: Write Optimized and Scalable Code
TASK #1:
What is Data Structure and Algorithm? What is the purpose of study Data Structure and
Algorithm? What are the applications of data structure?
SOLUTION:
A data structure is a named location that can be used to store and organize data. And an algorithm is a
collection of steps to solve a particular problem. Learning data structures and algorithms allow us to
write efficient and optimized computer programs.
Purpose of study Data Structure and Algorithm:
• Write optimized and scalable code - Once you have knowledge about different data
structures and algorithms, you can determine which data structure and algorithm to choose in
various conditions.
• Effective use of time and memory - Having knowledge about data structures and
algorithms will help you write codes that run faster and require less storage.
• Better job opportunities - Data structures and algorithms questions are frequently asked in
job interviews of various organizations including Google, Facebook, and so on.
Task #2:
Write a note on function definition, function prototype and function declaration with
proper syntax and figure.
SOLUTION:
A function is a group of statements that together perform a task. Every C/C++ program has at least one
function, which is main(), and all the most trivial programs can define additional functions.
You can divide up your code into separate functions. How you divide up your code among different
functions is up to you, but logically the division is such that each function performs a specific task.
Page 2 of 9
A function declaration tells the compiler about a function's name, return type, and parameters. A
function definition provides the actual body of the function.
A function prototype is simply the declaration of a function that specifies function's name, parameters
and return type. It does not contain function body. A function prototype gives information to the
compiler that the function may later be used in the program.
Defining a Function
A function definition in C programming consists of a function header and a function body. Here
are all the parts of a function −
• Return Type − A function may return a value. The return_type is the data type of the value
the function returns. Some functions perform the desired operations without returning a value.
In this case, the return_type is the keyword void.
• Function Name − This is the actual name of the function. The function name and the
parameter list together constitute the function signature.
• Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value
to the parameter. This value is referred to as actual parameter or argument. The parameter list
refers to the type, order, and number of the parameters of a function. Parameters are optional;
that is, a function may contain no parameters.
• Function Body − The function body contains a collection of statements that define what the
function does.
TASK #3:
Write a program which display your name and roll no in function named as mydata.
SOLUTION:
#include <iostream>
using namespace std;
void mydata()
{
cout<<"Name: KHAWAR KHALIL\n";
cout<<"Roll No: 19-EE-147";
}
int main()
{
mydata();
}
Page 3 of 9
TASK #4:
Write a program which take two inputs from user, pass them in a function and print the
sum in a function.
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
int x,y;
cout<<"Enter two numbers: ";
cin>>x>>y;
sum(x,y);
}
Page 4 of 9
TASK #5:
Write a program which take input from user, pass it to function and check whether the
number is even or odd.
SOLUTION:
#include <iostream>
using namespace std;
void EVEN_ODD(int a)
{
if(a%2==0) cout<<"Number is Even";
else cout<<"Number is Odd";
}
int main()
{
int x;
cout<<"Enter any number: ";
cin>>x;
EVEN_ODD(x);
}
Page 5 of 9
TASK #6:
Write a program which take five values as input from user, pass the values to function,
calculate their average value and return the result to main function to display.
SOLUTION:
#include <iostream>
using namespace std;
float avg(float n1, float n2, float n3, float n4, float n5)
{
return ((n1 + n2 + n3 + n4 + n5 )/ 5);
}
int main()
{
float i1, i2, i3, i4, i5, result;
cout<<"Enter five numbers: ";
cin>>i1>>i2>>i3>>i4>>i5;
result = avg(i1,i2,i3,i4,i5);
cout<<"Average is: "<<result;
}
Page 6 of 9
TASK #7:
Write a program which take six inputs from user in array and display them in function.
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
int arr[6];
cout<<"Enter six values: ";
for(int i=0; i<6; i++)
cin>>arr[i];
display(arr);
}
Page 7 of 9
TASK #8:
Write a program that takes input of seven integers in an array and passes the array to a
function, find the maximum number from the array and pass it to main function.
SOLUTION:
#include<iostream>
using namespace std;
int main ()
{
int arr[7], max;
cout << "Enter seven elements in array: ";
for (int i = 0; i < 7; i++)
cin >> arr[i];
max_num(arr);
cout << "Largest element : " << max_num(arr);
}
Page 8 of 9
TASK #9:
Write a program that takes input of ten integers in an array and passes array to a function.
The function counts even numbers in array and returns the result to main function to
display it.
SOLUTION:
#include<iostream>
using namespace std;
return j;
}
int main ()
{
int arr[10], count;
cout << "Enter ten elements in array: ";
for (int i = 0; i < 10; i++)
count = even_func(arr);
cout << "Even numbers are ""\"" << count <<"\""<<" in this array.";
}
Page 9 of 9
TASK #10:
What are benefits of using the functions and what are the applications?
SOLUTION:
The benefits of using functions are:
• Avoid repetition of codes.
• Increases program readability.
• Divide a complex problem into simpler ones.
• Reduces chances of error.
• Modifying a program becomes easier by using function.
Functions are used to perform certain actions, and they are important for reusing code: Define the code
once and use it many times.