submitted by: 079BCH047
079BCH025
079BCH031
047BCH039
047BCH048
2079 back
1.Explain the program development and compilation process in detail. Draw
flowchart to find all the possible roots of quadratic equation.
Program Development and Compilation Process
1. Requirement Analysis: This is the first phase where you determine what
exactly you want your software to achieve. For instance, you might want a
program to find all possible roots of a quadratic equation.
2. Design: Here, you design how your software will look and operate. This might
involve drawing up flowcharts, deciding on algorithms, and planning out the user
interface.
3. Coding: This is the phase where you write the actual code using a programming
language such as C++, Java, Python, etc. This code is written in text files called
source files.
4. Compilation: If you're using a compiled language (like C++ or Java), the next
step is to compile the source code. A compiler is a tool that translates the
human-readable source code into machine code or intermediate code. This
produces an object file (like .o or .obj for C++ or .class for Java).
5. Linking: For languages like C++, after compilation, there's a linking phase. This
step combines multiple object files (if there are any) and links them with libraries
to produce the final executable.
6. Execution: Now, the program is ready to be executed. If the program was
written correctly and there are no runtime errors, it will produce the expected
output.
7. Testing & Debugging: If the program doesn't work as expected, it's back to the
coding phase to identify and fix bugs. Debugging tools can be used to step
through code and identify where things are going wrong.
8. Deployment: Once you're satisfied with your program, you can deploy or
distribute it to users. This might involve packaging it in an installer, uploading it to
a website, or distributing it through a software repository.
Flowchart to calculate the roots of quadratic equation is shown below in figure:
2.Describe fundamental data types in the C programming language. What are
relational and logical operators?
The fundamental data types are:
Int data type: The int data type is used to store the integer values. Any
number that falls in the range of -∞ to +∞ is said to be an integer. For
example 0, 10, 105, -15, -103, etc are all integers. However, integers can
be either negative or positive. And that’s where we use data type
modifiers, which we will be going to discuss later in this article.
Float data type: It is used to store the floating-point numbers. The numbers that have a
fractional part are called floating-point numbers. For example, 3.0, 5.57, -31.2, -3.12, etc
are all floating-point numbers. The major advantage floating numbers over integers is
that they can represent a much larger and wider range of digits as compared to int data
type.
Char data type: char data type is used to store the characters. The characters stored
in a variable of char data type have a value equivalent to its integer code. These codes
are often called American Standard Code For Information Interchange or ASCII codes.
The ASCII code for upper case characters A to Z starts from 65 to 90 and for lower case
characters a to z, it starts from 97 to 122.
Double data type: double data type is also used to store floating-point numbers. But
still, it is considered as a distinct data type because of two reasons. The first one is that
it occupies twice as much memory as type float. The second reason is that it is used to
store a much larger range of floating-point numbers than a float data type.
Relational operators are used to compare two values in C language. It checks the
relationship between two values. If relation is true, it returns 1. However, if the relation is
false, it returns 0.
Here is the table of relational operators in C language
Operators Operator Name
== Equal to
> Greater than
< Less than
!= Not equal to
>= Greater than or equal to
<= Less than or equal to
Logical operators are used to perform logical operations. It returns 0 or 1 based on the
result of condition, whether its true or false. These operators are used for decision making
in C language.
Here is the table of logical operators in C language,
Operators Meaning of Operators Results
&& Logical AND True when all operands are true
|| Logical OR True only if either one operand is true
! Logical NOT True when operand is zero
3.Explain how scanf() and printf() are used. Write the syntax and use of gets(),
getchar(), scanf() .
The scanf() Function:The printf() function is used to output data to the console or a
file. It is used to display text, numbers, and other data types in a user-friendly format.
Here is an example of how the printf() function is used:
The scanf() Function:The scanf() function is used to read data from the console or a
file. It is used to input text, numbers, and other data types from the user. Here is an
example of how the scanf() function is used:
gets() : it is a pre-defined function in C which is used to read a string or a text line. And
store the input in a well-defined string variable. The function terminates its reading
session as soon as it encounters a newline character.
getchar(): In C is a function used to take the input of a single character from the
standard input stream present in the stdin library. The getchar in C returns the unsigned
char cast to the int value of the character it reads and does not take any parameter.
Scanf():The scanf() function is a commonly used input function in the C programming
language. It allows you to read input from the user or from a file and store that input in
variables of different data types.
4. Discuss the difference between whole and dowhile structure with example.
Write the program to find the sum of the following series up to n terms.
1 - X1/1! + X2/2! - ... Xn/n!
While Loop Do While Loop
The Keyword “while” The keyword “do while” is used.
is used.
The control statement is The control statement is present
executed before the loop body. after the loop body.
The loop body executes once, even
The loop body executes when
if the condition
the condition becomes true.
is true or false.
It follows a top-down approach. It follows a bottom-up approach.
For single statements, brackets
Brackets are always required.
are not required.
#include <stdio.h>
void main()
{
float x,sum,no_row;
int i,n;
printf("Input the value of x :");
scanf("%f",&x);
printf("Input number of terms : ");
scanf("%d",&n);
sum =1; no_row = 1;
for (i=1;i<n;i++)
{
no_row = no_row*x/(float)i;
sum =sum+ no_row;
}
printf("\nThe sum is : %f\n",sum);
}
5.Write the necessary condition for a function to be recursive. write a program to
generate Fibonacci series upto n terms uaing function.
A recursive function must have at least one condition where it will stop calling itself, or
the function will call itself indefinitely until throws an error.
In=nclude<stdio.h>
void fibonacciSeries(int range)
{
int a=0, b=1, c;
while (a<=range)
{
printf("%d\t", a);
c = a+b;
a = b;
b = c;
}
}
int main()
{
int range;
printf("Enter range: ");
scanf("%d", &range);
printf("The fibonacci series is: \n");
fibonacciSeries(range);
return 0;
}
6.Why do we need array in programming? Write a program to display the addition
of two matrix using function.
Arrays help maintain large sets of data under a single variable name to
avoid confusion that can occur when using several variables. Organizing
data elements: Different array algorithms, like bubble sort, selection sort
and insertion sort, can help you organize various data elements clearly
and efficiently.
#include<stdio.h>
#include<conio.h>
void show_matrix(int mat[3][3]);
void add_matrix(int matA[3][3], int matB[3][3], int matSum[3][3]);
int main()
{
int x[3][3], y[3][3], z[3][3];
int i, j;
printf("\nEnter elements of matrix 1:\n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
printf("Enter elements a[%d][%d]: ", i + 1, j + 1);
scanf("%d", &x[i][j]);
}
}
printf("\nEnter elements of matrix 2:\n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
printf("Enter elements b[%d][%d]: ", i + 1, j + 1);
scanf("%d", &y[i][j]);
}
}
add_matrix(x,y,z);
printf("\nFirst matrix is :\n");
show_matrix(x);
printf("\nSecond matrix is :\n");
show_matrix(y);
printf("\nNew addition matrix is :\n");
show_matrix(z);
getch();
return 0;
}
void add_matrix(int matA[3][3], int matB[3][3], int matSum[3][3])
{
int r,c;
for(r=0; r<3; r++)
{
for(c=0; c<3; c++)
matSum[r][c]=matA[r][c]+matB[r][c];
}
}
void show_matrix(int mat[3][3])
{
int r,c;
for(r=0; r<3; r++)
{
for(c=0; c<3; c++)
printf(" %d",mat[r][c]);
printf("\n");
}
}
8.How is array related with pointer? Write a program to read a string
containing letters, numbers and special characters, transfer letters
contained in it into another string using pointer, finally display the second
string containing only alphabets.
Array is related with pointer as:
An array name acts like a constant pointer to its first element.
Array elements can be accessed indirectly via pointers.
Arithmetic operations can be performed on pointers to traverse an array.
Pointers can be incremented or decremented to move between array
elements.
The relation between arrays and pointers forms the basis for dynamic
memory allocation in C/C++.
#include <stdio.h>
#include <conio.h>
int main() {
char input[100], output[100];
char *iptr, *optr;
clrscr();
printf("Enter the string: ");
gets(input);
iptr = input;
optr = output;
while (*iptr != '\0') {
if ((*iptr >= 'A' && *iptr <= 'Z') || (*iptr >= 'a' && *iptr <= 'z')) {
*optr = *iptr;
optr++;
}
iptr++;
}
*optr = '\0';
printf("String containing only alphabets: %s\n", output);
getch();
return 0;
}
9.Discuss “a” “r” “w” modes . Write a program that reads number from a file
containing series of numbers and separates odd numbers from even numbers
and writes them on two separate files.
"r" (read mode):
Purpose: Opens a file for reading.
Behavior: The file must exist, or the open operation will fail.
File position: The file position indicator (i.e., where the next read will start) is set
to the beginning of the file.
If you try to write to a file opened in "r" mode, the behavior is undefined. You're
not supposed to write to a file in this mode.
"w" (write mode):
Purpose: Opens a file for writing.
Behavior: If the file already exists, it will be truncated to zero length, essentially
discarding any existing data in the file. If the file does not exist, it will be created.
File position: The file position indicator is set to the beginning of the file.
This mode allows you to write to the file. If you try to read from a file opened in
"w" mode, the behavior is typically undefined.
"a" (append mode):
Purpose: Opens a file for appending.
Behavior: If the file exists, data written to the file will be appended to the end,
ensuring existing data remains intact. If the file does not exist, it will be created.
File position: The file position indicator is set to the end of the file, so any
subsequent write operations will append data to the file.
While you can read from a file opened in "a" mode in some implementations, it's
not guaranteed or standard behavior, and it's generally not recommended. If you
need to read and write to a file while ensuring that writes always append data,
the "a+" mode should be used.
#include <stdio.h>
#include <conio.h>
int main() {
FILE *sourceFile, *oddFile, *evenFile;
int num;
sourceFile = fopen("numbers.txt", "r");
if (sourceFile == NULL) {
printf("Unable to open the file.\n");
getch();
return 1;
}
oddFile = fopen("odd.txt", "w");
evenFile = fopen("even.txt", "w");
if (oddFile == NULL || evenFile == NULL) {
printf("Unable to create output files.\n");
fclose(sourceFile);
getch();
return 1;
}
while (fscanf(sourceFile, "%d", &num) == 1) {
if (num % 2 == 0) {
fprintf(evenFile, "%d\n", num);
} else {
fprintf(oddFile, "%d\n", num);
}
}
fclose(sourceFile);
fclose(oddFile);
fclose(evenFile);
printf("Numbers separated successfully!\n");
getch();
return 0;
}
10.How are one dimensional array declared in FORTRAN. WAP in FORTRAN to
read and compute the transpose of any matrix.
In FORTRAN, a one-dimensional array is declared within a type declaration statement,
specifying the type of the array elements followed by the array name and its dimension
in parentheses. For instance, INTEGER :: myArray(10) declares a one-dimensional
array named myArray with 10 elements of type INTEGER. The specified size, 10 in this
case, determines the number of elements the array can hold.
PROGRAM TransposeMatrix
IMPLICIT NONE
INTEGER :: i, j, rows, cols
REAL, ALLOCATABLE :: matrix(:,:), transpose(:,:)
! Read matrix dimensions
WRITE(*,*) 'Enter number of rows:'
READ(*,*) rows
WRITE(*,*) 'Enter number of columns:'
READ(*,*) cols
! Allocate matrix and its transpose
ALLOCATE(matrix(rows, cols))
ALLOCATE(transpose(cols, rows))
! Read matrix
WRITE(*,*) 'Enter elements of the matrix:'
DO i = 1, rows
DO j = 1, cols
READ(*,*) matrix(i, j)
ENDDO
ENDDO
! Compute transpose
DO i = 1, rows
DO j = 1, cols
transpose(j, i) = matrix(i, j)
ENDDO
ENDDO
! Display transpose
WRITE(*,*) 'Transpose of the matrix:'
DO i = 1, cols
DO j = 1, rows
WRITE(*,'(F5.2, 2X)', ADVANCE='NO') transpose(i, j)
ENDDO
WRITE(*,*)
ENDDO
DEALLOCATE(matrix)
DEALLOCATE(transpose)
END PROGRAM TransposeMatrix
THE END