Noakhali Science and Technology University
Noakhali - 3814
DEPARTMENT OF applied mathematics
ASSIGNMENT-01
Course Title: Programming With C/C++
Course Code: A.MTH-3107
Submitted By: Submitted To:
MAHAFUJ RAYHAN MD. Abdul Karim
Roll: ASH2206010M Assistant Professor, Department of
Applied Mathematics, NSTU
DATE OF SUBMISSION: 25 August, 2025
Q1. A class of n students take a semester final examination in m subjects. Write a
program to read the marks obtained by each student in various subjects and to
compute and print the total marks obtained by each of them.
Ans:
#include <stdio.h>
main()
{
//declaration section
int m,n,x,y;
printf("how many students?\n");
scanf("%d",&n);
printf("how many subjects?\n");
scanf("%d",&m);
float sub[m],student[n],total;
//data input of the students
for(x=0;x<n;x++){
total=0.0;
for(y=0;y<m;y++){
printf("Enter the number of the student %d in the subject %d\n",x+1,y+1);
scanf("%f",&sub[y]);
total=total+sub[y];
};
student[x]=total;
};
//output section
for(x=0;x<n;x++){
printf("the total mark of student %d is %f\n",x+1,student[x]);
};
return 0;
}
Output:
Q2. Discuss about array in C programming language.
Ans:
In C programming, an array is a data structure used to store a fixed-size sequential
collection of elements of the same data type. This means an array can hold multiple
values, but all those values must be of the same type (e.g., all integers, all characters,
or all floating-point numbers).
Characteristics of Arrays in C:
1. Homogeneous Data Type:
All elements within an array must be of the same data type (e.g., all integers, all
characters, all floats).
2. Contiguous Memory Allocation:
Array elements are stored sequentially in memory, meaning they occupy adjacent
memory addresses. This allows for efficient access to elements.
3. Fixed Size:
Once declared, the size of an array is fixed and cannot be changed during program
execution.
4. Zero-Based Indexing:
Array elements are accessed using an index, which starts from 0 for the first element.
For an array of size n, the valid indices range from 0 to n-1.
Declaration and Initialization
Arrays are declared by specifying the data type, array name, and size (number of
elements) within square brackets.
Syntax:
dataType arrayName[arraySize];
Example:
To declare an integer array named numbers with 5 elements:
int numbers[5];
Arrays can be initialized during declaration:
int numbers[5] = {10, 20, 30, 40, 50};
Types of Arrays
1. One-dimensional array:
A list of items can be given one variable name using only one subscript, and such a
variable is called a single-subscripted variable or a one-dimensional array.
The general form of array declaration is:
type variable-name[size];
2. Two-dimensional array:
Two-dimensional arrays are declared as follows:
type array-name[row_size][column_size];
3. Multi-dimensional array:
C allows arrays of three or more dimensions. The exact limit is determined by the
compiler. The general form of a multi-dimensional array is:
type array-name[s1][s2]...[sm];
where, Si is the size of the i-th dimension.
Q3. Write a program to arrange n numbers in ascending order by bubble sort.
Ans:
#include <stdio.h>
int main(void)
{
// Declaration section
int i, j, pass, size, temp;
printf("Enter the number of elements: ");
scanf("%d", &size);
int array[size]; // Array declaration
// Data input section
for (i = 0; i < size; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &array[i]);
}
// Sorting section (Bubble Sort - Ascending order)
for (pass = 0; pass < size - 1; pass++) {
for (j = 0; j < size - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
};
};
// Display array after each pass
printf("After pass %d: ", pass + 1);
for (i = 0; i < size; i++) {
printf("%d ", array[i]);
};
printf("\n");
};
// Sorted output (ascending order)
printf("\nFinal sorted list in ascending order:\n");
for (i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
};
Output:
Q4. Describe about structures of C programming language.
Ans:
The structure of C programming language:
1.Documentation Section
2.Link Section
3.Definition Section
4.Global Declaration Section
5.main() Function Section
{
Declaration part
Executable part
}
6.Subprogram Section
{
Function 1
Function 2
...
Function n (User-defined functions)
}
The structure of a C program typically consists of the following sections:
Documentation Section
The documentation section consists of a set of comment lines giving the name of the
program, the author, and other details which the programmer might want to use later.
Link Section
The link section provides instructions to the compiler to link functions from the system
library.
Definition Section
The definition section defines all symbolic constants.
Global Declaration Section
Some variables are used in more than one function. Such variables are called global
variables and are declared in the global declaration section, which is outside all the
functions. This section also declares all user-defined functions.
Main() Function Section:Every C program must have one main() function section. This
section contains two parts:
1.Declaration part
2.Executable part
The declaration part declares all the variables used in the executable part. There must
be at least one statement in the executable part. Both parts must appear between the
opening and closing braces.
Program execution begins at the opening brace and ends at the closing brace. The
closing brace of the main() function section marks the logical end of the program. All
statements in the declaration and executable parts must end with a semicolon (;).
Subprogram Section
The subprogram section contains all the user-defined functions that are called in the
main function. These functions are usually placed immediately after the main
function, although they may appear in any order.
Q5. What is the difference between arrays and structures?
Ans: Differences between Arrays and Structures in C
Data type of elements
Array: All elements must be of the same data type.
Structure: Members can be of different data types.
Access method
Array: Elements are accessed using indices.
Structure: Members are accessed using the dot (.) operator.
Memory allocation
Array: Elements are stored in contiguous memory locations.
Structure: Members may be stored in contiguous memory, but each member
can have different sizes.
Size
Array: Can only store a fixed number of elements of the same type.
Structure: Can store a combination of different types in a single unit.
Usage
Array: Useful for storing a list of similar items, like numbers or characters.
Structure: Useful for storing a record or entity with multiple attributes, like
student details (name, age, marks).
Operations
Array: Arithmetic and looping operations can be performed easily on elements.
Structure: Arithmetic operations cannot be performed directly on the structure;
individual members must be accessed.
Homogeneity vs Heterogeneity
Array: Homogeneous (all elements of same type).
Structure: Heterogeneous (members of different types).
Q6. Write a program to calculate the Fibonacci series for the first 10 numbers.
Ans:
#include <stdio.h>
main()
//declaration part
int a=0,b=1,c,i;
printf("%d%d",a,b);
for(i=2;i<10;i++)
c=a+b;
printf("%d",c);
a=b;
b=c;
}
printf("\n");
return 0;
Output:
Q7. Write a program to construct a structure of a person name, date of joining and
salary.
Ans:
#include <stdio.h>
struct personal {
char name[20];
int day;
char month[10];
int year;
float salary;
};
int main() {
struct personal person;
printf("Input Values (name day month year salary):\n");
scanf("%19s %d %9s %d %f",
person.name,
&person.day,
person.month,
&person.year,
&person.salary);
printf("%s %d %s %d %.2f\n",
person.name,
person.day,
person.month,
person.year,
person.salary);
return 0;
}
Output: