Disclaimer
The solution of question or notes are written by either student or taken from some publications , so it is the responsibility of viewer to check whether answers are correct or incorrect.
2020-Solution
Ans .2- a) Methods for problem solving
Problem-solving methods are primarily designed to help a group or team through a process of first identifying problems and challenges, ideating possible solutions, and then evaluating the most suitable.
Finding effective solutions to complex problems isnโt easy, but by using the right process and techniques, you can help your team be more efficient in the process.
Identify problems?
Before you can move towards finding the right solution for a given problem, you first need to identify and define the problem you wish to solve.
Here, you want to clearly articulate what the problem is and allow your group to do the same. Remember that everyone in a group is likely to have differing perspectives and alignment is necessary in order to help the group move forward.
Identifying a problem accurately also requires that all members of a group are able to contribute their views in an open and safe manner. It can be scary for people to stand up and contribute, especially if the problems or challenges are emotive or personal in nature. Be sure to try and create a psychologically safe space for these kinds of discussions
Problem-solving methods
In this section, weโll look at in-depth problem-solving methods that provide a complete end-to-end process for developing effective solutions. These will help guide your team from the discovery and definition of a problem through to delivering the right solution.
If youโre looking for an all-encompassing method or problem-solving model, these processes are a great place to start. Theyโll ask your team to challenge preconceived ideas and adopt a mindset for solving problems more effectively.
Six Thinking Hats
Lightning Decision Jam
Problem Definition Process
The 5 Whys
World Cafe
Discovery & Action Dialogue
Design Sprint 2.0
Open Space Technology
b) programming algorithm
An algorithm is a well-defined sequential computational technique that accepts a value or a collection of values as input and produces the output(s) needed to solve a problem.
Or we can say that an algorithm is said to be accurate if and only if it stops with the proper output for each input instance.
Example: algorithm to multiply 2 numbers and print the result:
Step 1: Start Step 2: Get the knowledge of input. Here we need 3 variables; a and b will be the user input and c will hold the result. Step 3: Declare a, b, c variables. Step 4: Take input for a and b variable from the user. Step 5: Know the problem and find the solution using operators, data structures and logic
We need to multiply a and b variables so we use * operator and assign the result to c. That is c <- a * b
Step 6: Check how to give output, Here we need to print the output. So write print c Step 7: End
Ans .3- a) Difference between compilation and execution of program
First of all ,computers are electronic device know only one language that is machine language.
It contains only zeros and ones.
so if you need to instruct a computer you need to give instructions in binary form that is ones and zeros.
For a human it is very difficult to talk in this language. So we create a software(software or executable file always contain binary information or instructions to computer) which will convert our known language( high level language) into machine level language. thus computer can work.
This software is known as compilers.
compiler is a software which converts high level language into machine level language. And this process is known as compilation.
after compilation we get a file known as object file which is machine language. Object file can be directly loaded into computer memory and execute or run that. The process of loading object file and running that file is known as executing.
b) effect of warning and syntax errors on program
In computer science, a syntax error is an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular programming language.
For compiled languages, syntax errors are detected at compile-time. A program will not compile until all syntax errors are corrected. For interpreted languages, however, a syntax error may be detected during program execution, and an interpreter's error messages might not differentiate syntax errors from errors of other kinds.
There is some disagreement as to just what errors are "syntax errors". For example, some would say that the use of an uninitialized variable's value in Java code is a syntax error, but many others would disagree[1][2] and would classify this as a (static) semantic error.
c) variables and datatypes in C
Variables
Variables are containers for storing data values, like numbers and characters.
In C, there are different types of variables (defined with different keywords), for example:
int - stores integers (whole numbers), without decimals, such as 123 or -123
float - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
Data Types
As explained in the Variables chapter, a variable in C must be a specified data type, and you must use a format specifier inside the printf() function to display it:
Basic Data Types
The data type specifies the size and type of information the variable will store.
In this tutorial, we will focus on the most basic ones:
Data Type Size Description
int 2 or 4 bytes Stores whole numbers, without decimals
float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 6-7 decimal digits
double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits
char 1 byte Stores a single character/letter/number, or ASCII values
Ans .4- a) header file in C language
A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.
The #include preprocessor directive is used to paste code of given file into current file. It is used include system-defined and user-defined header files. If included file is not found, compiler renders error.
By the use of #include directive, we provide information to the preprocessor where to look for the header files. There are two variants to use #include directive.
1 stdio.h Input/Output functions
2 conio.h Console Input/Output functions
3 stdlib.h General utility functions
4 math.h Mathematics functions
b) flowchart to find average of 10 numbers
c) conditional operator in c with examples
The conditional operator is also known as a ternary operator. The conditional statements are the decision-making statements which depends upon the output of the expression. It is represented by two symbols, i.e., '?' and ':'.
As conditional operator works on three operands, so it is also known as the ternary operator.
The behavior of the conditional operator is similar to the 'if-else' statement as 'if-else' statement is also a decision-making statement.
Let's understand the ternary or conditional operator through an example.
#include <stdio.h>
int main()
{
int age; // variable declaration
printf("Enter your age");
scanf("%d",&age); // taking user input for age variable
(age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting")); // conditional operator
return 0;
}
Ans .5- a) loops with break and continue
Loops
The looping can be defined as repeating the same process multiple times until a specific condition satisfies. There are three types of loops used in the C language. In this part of the tutorial, we are going to learn all the aspects of C loops.
Why use loops in C language?
The looping simplifies the complex problems into the easy ones. It enables us to alter the flow of the program so that instead of writing the same code again and again, we can repeat the same code for a finite number of times. For example, if we need to print the first 10 natural numbers then, instead of using the printf statement 10 times, we can print inside a loop which runs up to 10 iterations.
Advantage of loops in C
1) It provides code reusability.
2) Using loops, we do not need to write the same code again and again.
3) Using loops, we can traverse over the elements of data structures (array or linked lists).
Types of C Loops
There are three types of loops in C language that is given below:
do while
while
for
The break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops.
The continue statement in C language is used to bring the program control to the beginning of the loop. The continue statement skips some lines of code inside the loop and continues with the next iteration. It is mainly used for a condition so that we can skip some code for a particular condition.
#include<stdio.h>
int main(){
int i=1;//initializing a local variable
//starting a loop from 1 to 10
for(i=1;i<=10;i++){
if(i==5){//if value of i is equal to 5, it will continue the loop
continue;
if(i==9){//if value of i is equal to 5, it will continue the loop
break;
}
printf("%d \n",i);
}//end of for loop
return 0;
}
b) Program to find median of numbers in c
#include < stdio.h >
#include < conio.h >
#include < sdtlib >
#define N 10
main ( ) {
int i , j , n ;
float median , a [ N ] , t ;
printf ( " ?nter the number of items \ n " ) ;
scanf ( " % d " , & n ) ;
/* Reading items into array a */
printf ( " Input % d values \ n " , n ) ;
for ( i = 1 ; i < = n ; i + + )
scanf ( " % f " , & a [ i ] ) ;
/* Sorting begins */
for ( i = 1 ; i < = n - 1 ; i + + ) { / * Trip - i begins * /
for ( j = 1 ; j < = n - i ; j + + ) {
if ( a [ j ] < = a [ j + 1 ] ) { / * Interchanging values * /
t = a [ j ] ;
a [ j ] = a [ j + 1 ] ;
a [ j + 1 ] = t ;
}
else
continue ;
}
} / * sorting ends * /
/ * calculation of median * /
if ( n % 2 = = 0 )
median = ( a [ n / 2 ] + a [ n / 2 + 1 ] ) / 2.0 ;
else
median = a [ n / 2 + 1 ] ;
/ * Printing * /
for ( i = 1 ; i < = n ; i + + )
printf ( " % f " , a [ i ] ) ;
printf ( " \ n \ n Median is % f \ n " , median ) ;
}
Ans .6- a) function with its types
Functions
In c, we can divide a large program into the basic building blocks known as function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known as procedure or subroutine in other programming languages.
SN C function aspects Syntax
1 Function declaration return_type function_name (argument list);
2 Function call function_name (argument_list)
3 Function definition return_type function_name (argument list) {function body;}
Types of Functions
There are two types of functions in C programming:
Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.
b) fibbonecci series
Let's see the fibonacci series program in c without recursion.
#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);//printing 0 and 1
for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}
c) three ways to read string from prompt
Method 1 : Using gets Syntax : char *gets(char *str)
#include <stdio.h>
int main()
{
charstr[20];
gets(str);
printf("%s", str);
return0;
}
Method 2 : To overcome the above limitation, we can use fgets as : Syntax : char *fgets(char *str, int size, FILE *stream) Example : fgets(str, 20, stdin); as here, 20 is MAX_LIMIT according to declaration.
#include <stdio.h>
#define MAX_LIMIT 20
int main()
{
char str[MAX_LIMIT];
fgets(str, MAX_LIMIT, stdin);
printf("%s", str);
return 0;
}
Method 3 : Using %[^\n]%*c inside scanf Example : scanf(โ%[^\n]%*cโ, str);
#include <stdio.h>
int main()
{
charstr[20];
scanf("%[^\n]%*c", str);
printf("%s", str);
return0;
}
Ans .7 - a) Parameter/argument passing technique in function
There are different ways in which parameter data can be passed into and out of methods and functions. Let us assume that a function B() is called from another function A(). In this case A is called the โcaller functionโ and B is called the โcalled function or callee functionโ. Also, the arguments which A sends to B are called actual arguments and the parameters of B are called formal arguments.
Terminology
ยท Formal Parameter : A variable and its type as they appear in the prototype of the function or method.
ยท Actual Parameter : The variable or expression corresponding to a formal parameter that appears in the function or method call in the calling environment.
Important methods of Parameter Passing
1. Pass By Value: This method uses in-mode semantics. Changes made to formal parameter do not get transmitted back to the caller. Any modifications to the formal parameter variable inside the called function or method affect only the separate storage location and will not be reflected in the actual parameter in the calling environment. This method is also called as call by value.
// C program to illustrate
// call by value
#include <stdio.h>
void func(int a, int b)
{
a += b;
printf("In func, a = %d b = %d\n", a, b);
}
int main(void)
{
int x = 5, y = 7;
// Passing parameters
func(x, y);
printf("In main, x = %d y = %d\n", x, y);
return 0;
}
2. Pass by reference(aliasing): This technique uses in/out-mode semantics. Changes made to formal parameter do get transmitted back to the caller through parameter passing. Any changes to the formal parameter are reflected in the actual parameter in the calling environment as formal parameter receives a reference (or pointer) to the actual data. This method is also called as call by reference. This method is efficient in both time and space.
// C program to illustrate
// call by reference
#include <stdio.h>
void swapnum(int* i, int* j)
{
inttemp = *i;
*i = *j;
*j = temp;
}
int main(void)
{
inta = 10, b = 20;
// passing parameters
swapnum(&a, &b);
printf("a is %d and b is %d\n", a, b);
return 0;
}
b) largest number from array
#include <stdio.h>
int main()
{
//Initialize array
int arr[] = {25, 11, 7, 75, 56};
//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr[0]);
//Initialize max with first element of array.
int max = arr[0];
//Loop through the array
for (int i = 0; i < length; i++) {
//Compare elements of array with max
if(arr[i] > max)
max = arr[i];
}
printf("Largest element present in given array: %d\n", max);
return 0;
}
c) difference between structure and union
Differences between Structure and Union are as shown below in tabular format as shown below as follows:
Meet alexa
Ans 8. a) Dynamic memory allocation in c
Since C is a structured language, it has some fixed rules for programming. One of them includes changing the size of an array. An array is a collection of items stored at contiguous memory locations.
As it can be seen that the length (size) of the array above made is 9. But what if there is a requirement to change this length (size). For Example,
ยท If there is a situation where only 5 elements are needed to be entered in this array. In this case, the remaining 4 indices are just wasting memory in this array. So there is a requirement to lessen the length (size) of the array from 9 to 5.
ยท Take another situation. In this, there is an array of 9 elements with all 9 indices filled. But there is a need to enter 3 more elements in this array. In this case, 3 indices more are required. So the length (size) of the array needs to be changed from 9 to 12.
This procedure is referred to as Dynamic Memory Allocation in C. Therefore, C Dynamic Memory Allocation can be defined as a procedure in which the size of a data structure (like Array) is changed during the runtime. C provides some functions to achieve these tasks. There are 4 library functions provided by C defined under <stdlib.h> header file to facilitate dynamic memory allocation in C programming.
They are:
1. malloc()
2. calloc()
3. free()
4. realloc()
#include <stdio.h>
#include <stdlib.h>
int main()
{
// This pointer will hold the
// base address of the block created
int* ptr;
int n, i;
// Get the number of elements for the array
printf("Enter number of elements:");
scanf("%d",&n);
printf("Entered number of elements: %d\n", n);
// Dynamically allocate memory using malloc()
ptr = (int*)malloc(n * sizeof(int));
// Check if the memory has been successfully
// allocated by malloc or not
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
// Memory has been successfully allocated
printf("Memory successfully allocated using malloc.\n");
// Get the elements of the array
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
// Print the elements of the array
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}
return 0;
}
b) copy one file into another file in c
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}
Ans 9. a) structure in c
#include<stdio.h>
#include <string.h>
struct employee
{ int id;
char name[50];
}e1; //declaring e1 variable for structure
int main( )
{
//store first employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
//printing first employee information
printf( "employee 1 id : %d\n", e1.id);
printf( "employee 1 name : %s\n", e1.name);
return 0;
}
b) command line argument in c
It is possible to pass some values from the command line to your C programs when they are executed. These values are called command line arguments and many times they are important for your program especially when you want to control your program from outside instead of hard coding those values inside the code.
The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program. Following is a simple example which checks if there is any argument supplied from the command line and take action accordingly โ
#include <stdio.h>
int main( int argc, char *argv[] ) {
if( argc == 2 ) {
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 ) {
printf("Too many arguments supplied.\n");
}
else {
printf("One argument expected.\n");
}
}
Second hand laptop in minimum price
Kommentarer