Skip to main content

PROBLEM SOLVING AND PYTHON PROGRAMMING QUIZ

1) What is the first step in problem-solving? A) Writing code B) Debugging C) Understanding the problem D) Optimizing the solution Answer: C 2) Which of these is not a step in the problem-solving process? A) Algorithm development B) Problem analysis C) Random guessing D) Testing and debugging Answer: C 3) What is an algorithm? A) A high-level programming language B) A step-by-step procedure to solve a problem C) A flowchart D) A data structure Answer: B 4) Which of these is the simplest data structure for representing a sequence of elements? A) Dictionary B) List C) Set D) Tuple Answer: B 5) What does a flowchart represent? A) Errors in a program B) A graphical representation of an algorithm C) The final solution to a problem D) A set of Python modules Answer: B 6) What is pseudocode? A) Code written in Python B) Fake code written for fun C) An informal high-level description of an algorithm D) A tool for testing code Answer: C 7) Which of the following tools is NOT commonly used in pr...

Passing Parameters to Functions

Passing Parameters to Functions
There are two ways in which arguments or parameters can be passed to the called function.
Call by value The values of the variables are passed by the calling function to the called function. 
Call by reference The addresses of the variables are passed by the calling function to the called function.

Call by Value
In this method, the called function creates new variables to store the value of the arguments passed to it. Therefore, the called function uses a copy of the actual arguments to perform its intended task.
If the called function is supposed to modify the value of the parameters passed to it, then the change will be reflected only in the called function. In the calling function, no change will be made to the value of the variables. This is because all the changes are made to the copy of the variables and not to the actual variables. To understand this concept, consider the code given below. The function add() accepts an integer variable num and adds 10 to it. In the calling function, the value 
of num = 2. In add(), the value of num is modified to 12 but in the calling function, the change is not reflected.
#include <stdio.h>
void add(int n);
int main()
{
 int num = 2;
 printf("\n The value of num before calling the function = %d", num);
add(num);
printf("\n The value of num after calling the function = %d", num);
  return 0;
}
void add(int n)
{
n = n + 10;
printf("\n The value of num in the called function = %d", n);
}
Output
The value of num before calling the function = 2
The value of num in the called function = 12
The value of num after calling the function = 2

Following are the points to remember while passing arguments to a function using the call-by-value method:
* When arguments are passed by value, the called function creates new variables of the same data type as the arguments passed to it.
* The values of the arguments passed by the calling function are copied into the newly created variables.
* Values of the variables in the calling functions remain unaffected when the arguments are passed using the call-by-value technique.
Pros and cons
The biggest advantage of using the call-by-value technique is that arguments can be passed as variables, literals, or expressions, while its main drawback is that copying data consumes additional 
storage space. In addition, it can take a lot of time to copy, thereby resulting in performance penalty, especially if the function is called many times.

Call by Reference
When the calling function passes arguments to the called function using the call-by-value method, the only way to return the modified value of the argument to the caller is explicitly using the return statement. A better option is to pass arguments using the call-by-reference technique. 
In this method, we declare the function parameters as references rather than normal variables. 
When this is done, any changes made by the function to the arguments it received are also visible in the calling function.
To indicate that an argument is passed using call by reference, an asterisk (*) is placed after the type in the parameter list. 
Hence, in the call-by-reference method, a function receives an implicit reference to the argument, rather than a copy of its value. Therefore, the function can modify the value of the variable and that change will be reflected in the calling function as well. The following code illustrates this 
concept.
#include <stdio.h>
void add(int *);
int main()
{
int num = 2;
printf("\n The value of num before calling the function = %d", num);
add(&num);
printf("\n The value of num after calling the function = %d", num);
return 0;
}
void add(int *n)
{
*n = *n + 10;
printf("\n The value of num in the called function = %d", *n);
}
Output
The value of num before calling the function = 2
The value of num in the called function = 12
The value of num after calling the function = 12
Advantages
The advantages of using the call-by-reference technique of passing arguments include:
* Since arguments are not copied into the new variables, it provides greater time and space-efficiency.
* The function can change the value of the argument and the change is reflected in the calling function.
* A function can return only one value. In case we need to return multiple values, we can pass those arguments by reference, so that the modified values are visible in the calling function.
Disadvantages
However, the drawback of using this technique is that if inadvertent changes are caused to variables in called function then these changes would be reflected in calling function as original values would have been overwritten.Consider the code given below which swaps the value of two integers. Note the value of integers 
in the calling function and called function.
//This function swaps the value of two variables
#include <stdio.h>
void swap_call_val(int, int);
void swap_call_ref(int *, int *);
int main()
{
int a=1, b=2, c=3, d=4;
printf("\n In main(), a = %d and b = %d", a, b);
swap_call_val(a, b);
printf("\n In main(), a = %d and b = %d", a, b);
printf("\n\n In main(), c = %d and d = %d", c, d);
swap_call_ref(&c, &d);
printf("\n In main(), c = %d and d = %d", c, d);
return 0;
}
void swap_call_val(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
printf("\n In function (Call By Value Method) – a = %d and b = %d", a, b);
}
void swap_call_ref(int *c, int *d)
{
int temp;
temp = *c;
*c = *d;
*d = temp;
printf("\n In function (Call By Reference Method) – c = %d and d = %d", *c, *d);
}
Output
In main(), a = 1 and b = 2
In function (Call By Value Method) – a = 2 and b = 1
In main(), a = 1 and b = 2
In main(), c = 3 and d = 4
In function (Call By Reference Method) – c = 4 and d = 3
In main(), c = 4 and d = 3

Popular posts from this blog

Introduction to C Programs

INTRODUCTION The programming language ‘C’ was developed by Dennis Ritchie in the early 1970s at Bell Laboratories. Although C was first developed for writing system software, today it has become such a famous language that a various of software programs are written using this language. The main advantage of using C for programming is that it can be easily used on different types of computers. Many other programming languages such as C++ and Java are also based on C which means that you will be able to learn them easily in the future. Today, C is mostly used with the UNIX operating system. Structure of a C program A C program contains one or more functions, where a function is defined as a group of statements that perform a well-defined task.The program defines the structure of a C program. The statements in a function are written in a logical series to perform a particular task. The most important function is the main() function and is a part of every C program. Rather, the execution o...

Performance

Performance ( Optional ) * The I/O system is a main factor in overall system performance, and can place heavy loads on other main components of the system ( interrupt handling, process switching, bus contention, memory access and CPU load for device drivers just to name a few. ) * Interrupt handling can be relatively costly ( slow ), which causes programmed I/O to be faster than interrupt driven I/O when the time spent busy waiting is not excessive. * Network traffic can also loads a heavy load on the system. Consider for example the sequence of events that occur when a single character is typed in a telnet session, as shown in figure( And the fact that a similar group of events must happen in reverse to echo back the character that was typed. ) Sun uses in-kernel threads for the telnet daemon, improving the supportable number of simultaneous telnet sessions from the hundreds to the thousands.   fig: Intercomputer communications. * Rather systems use front-end processor...

Mathematics

MATHEMATICS           Mathematics is the science that deals with shapes, quantities and arrangements. Archmedes is known as the father of Mathematics (287BC-212BC). Mathematics seek and use patterns to formulates new conjuctures.They resove truth or false by using mathematical proof. Mathematics developed by counting, calculation, Measurements, Shapes and motion of physical objects.  Definition Mathematics has no general accepted definition. Until 18th century Aristotle defined mathematics as "the science of quantity". Many mathematicans take no interest in definition they simply say "Mathematics is what Mathematican do". Three leading definition of mathematics today are logicist, intutionist, and formalist. Logicist - In terms of Benjamin peirce, the definition of mathematics in terms of logic are "the science that draws necessary conclusion" and also said that " All mathematics is symbolic logic" by Mathematician Rusell. Intutionist - L.E.J.Bro...