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...

Input and Output Functions

INPUT AND OUTPUT FUNCTIONS
The most fundamental operation in a C program is to accept input values from a standard input device and output the data produced by the program to a standard output device. we can assign values to variables using the assignment operator ‘=’. For example,
int a = 3;
What if we want to assign value to variable a that is inputted from the user at run-time? This is done by using the scanf function that reads data from the keyboard. Similarly, for outputting results ofthe program, printf function is used that sends results to a terminal. Like printf and scanf, there are different functions in C that can carry out the input–output operations. These functions are collectively known as Standard Input/ Output Library. A program that uses standard input/output functions must contain the following statement at the beginning of the program:
#include <stdio.h>

scanf()
The scanf()function is used to read formatted data from the keyboard. The syntax of the scanf() function can be given as,
scanf ("control string", arg1, arg2, arg3...argn);
The control string specifies the type and format of the data that has to be obtained from the keyboard and stored in the memory locations pointed by the arguments, arg1, arg2, ...,argn. The prototype of the control string can be given as,
%[*][width][modifier]type
* is an optional argument that suppresses assignment of the input field. That is, it indicates that data should be read from the stream but ignored (not stored in the memory location).
width is an optional argument that specifies the maximum number of characters to be read. However, if the scanf function encounters a white space or an unconvertible character, input is 
terminated.
modifier is an optional argument (h, l, or L), which modifies the type specifier. 
Modifier h is used for short int or unsigned short int, l is used for long int, unsigned long int, or double values. Finally, L is used for long double data values.
type specifies the type of data that has to be read. It also indicates how this data is expected to be read from the user. The type specifiers for scanf function are given Table :  Type specifiers
Type      Qualifying Input
%c           For single characters
%d,          %i For integer values
%e,          %E,%f,%g,%G For floating point  numbers
%o            For octal numbers
%s             For a sequence of (string of) characters
%u             For unsigned integer values
%x,%X       For hexadecimal values
The scanf function ignores any blank spaces, tabs, and newlines entered by the user. The function simply returns the number of input fields successfully scanned and stored.
As we have not studied functions till now, understanding scanf function in depth will be a bit difficult here, but for now just understand that the scanf function is used to store values in memory locations associated with variables. For this, the function should have the address of the variables. The address of the variable is denoted by an & sign followed by the name of the variable. Look at the following code that shows how we can input value in a variable of int data type:
int num;
scanf(" %4d ", &num);
The scanf function reads first four digits into the address or the memory location pointed by num.

printf()
The printf function is used to display information required by the user and also prints the values of the variables. Its syntax can be given as:
printf ("control string", arg1,arg2,arg3,...,argn);
After the control string, the function can have as many arguments as specified in the control string. The control string contains format specifiers which are arranged in the order so that they 
correspond with the arguments in the variable list. It may also contain text to be printed such as instructions to the user, identifier names, or any other text to make the text readable.
Note that there must be enough arguments because if there are not enough arguments, then the result will be completely unpredictable. However, if by mistake you specify more number of 
arguments, the excess arguments will simply be ignored. The prototype of the control string can be given as below:
%[flags][width][.precision][modifier]type
Each control string must begin with a % sign. 
flags is an optional argument, which specifies output justification like decimal point, numerical sign, trailing zeros or octadecimal or hexadecimal prefixes. Below Table shows different types of flags with their descriptions.
Table : Flags in printf()
Flags      Description
–             Left–justify within the given field width
+             Displays the data with its numeric sign (either + or –)
#           Used to provide additional specifiers like o, x, X, 0, 0x, or 0X for
octal and hexadecimal values respectively for values different than zero
0          The number is left–padded with zeroes (0) instead of spaces.
width is an optional argument which specifies the minimum number of positions that the output characters will occupy. If the number of output characters is smaller than the specified width, then the output would be right justified with blank spaces to the left. However, if the number of characters is greater than the specified width, then all the characters would be printed.
precision is an optional argument which specifies the number of digits to print after the decimal point or the number of characters to print from a string. 
modifier field is same as given for scanf() function. 
type is used to define the type and the interpretation of the value of the corresponding argument. 
The type specifiers for printf function are given 
The most simple printf statement is
printf ("Welcome to the world of C language");
The function when executed prompts the message enclosed in the quotation to be displayed on the screen.
For float x = 8900.768, the following examples show output under different format specifications:
printf ("%f", x) 8 9 0 0 . 7 6 8
printf("%10f", x); 8 9 0 0 . 7 6 8
printf("%9.2f", x); 8 9 0 0 . 7 7
printf("%6f", x); 8 9 0 0 . 7 6 8

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...