Skip to main content
Главная страница » Football » Gibraltar U19 vs Georgia U19

Gibraltar U19 vs Georgia U19

Gibraltar U19

LLL
-

Georgia U19

WLL
Date: 2025-11-18
Time: 10:30
(FT)
Venue: Not Available Yet
Score: 0-1

Predictions:

MarketPredictionOddResult
Over 1.5 Goals63.40%(0-1) 1.06

Gibraltar U19 vs Georgia U1phuongdang94/Assigment-C<|file_sep
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}

# Assignment 2: Dynamic Memory Allocation

## **1. What is the difference between static and dynamic memory allocation?**
Static memory allocation is the memory allocation at compile time. The size of static memory is fixed and cannot be changed during program execution. Dynamic memory allocation is the memory allocation at run time. The size of dynamic memory can be changed during program execution.

## **2. What are the advantages and disadvantages of using dynamic memory allocation?**
The advantages of using dynamic memory allocation are:
* It allows programs to allocate only as much memory as they need.
* It allows programs to change the amount of allocated memory during execution.
* It enables the creation of data structures such as linked lists, trees, and graphs that can grow and shrink dynamically.
The disadvantages of using dynamic memory allocation are:
* It can lead to fragmentation of the heap, which can decrease performance.
* It requires more careful management of memory than static allocation.

## **3. Write a C program that uses dynamic memory allocation to create an array of integers with a user-specified size. The program should then fill the array with random numbers between 0 and 99 and print out the contents of the array.**

c
#include
#include
#include

int main() {
int n;
printf(“Enter the size of the array: “);
scanf(“%d”, &n);

int *arr = (int *) malloc(n * sizeof(int));

if (arr == NULL) {
printf(“Memory allocation failedn”);
return 1;
}

srand(time(NULL));
for (int i = 0; i < n; i++) {
arr[i] = rand() % 100;
}

printf("Array contents:n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("n");

free(arr);

return 0;
}

## **4. Modify your program from question 3 to allow the user to specify a range for the random numbers (e.g., between 50 and 100).**

c
#include
#include
#include

int main() {
int n, min, max;
printf(“Enter the size of the array: “);
scanf(“%d”, &n);
printf(“Enter the minimum value: “);
scanf(“%d”, &min);
printf(“Enter the maximum value: “);
scanf(“%d”, &max);

int *arr = (int *) malloc(n * sizeof(int));

if (arr == NULL) {
printf(“Memory allocation failedn”);
return 1;
}

srand(time(NULL));
for (int i = 0; i < n; i++) {
arr[i] = min + rand() % (max – min + 1);
}

printf("Array contents:n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("n");

free(arr);

return 0;
}

## **5. Write a C program that uses dynamic memory allocation to create a matrix with user-specified dimensions. The program should then fill the matrix with random numbers between 0 and 99 and print out its contents in a tabular format.**

c
#include
#include
#include

void print_matrix(int **matrix, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%dt", matrix[i][j]);
}
printf("n");
}
}

int main() {
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);

int **matrix = (int **) malloc(rows * sizeof(int *));

if(matrix == NULL) {
printf("Memory allocation failedn");
return 1;
}

for(int i = 0; i < rows; i++) {
matrix[i] = (int *) malloc(cols * sizeof(int));
if(matrix[i] == NULL) {
printf("Memory allocation failedn");
return 1;
}
}

srand(time(NULL));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = rand() % 100;
}
}

printf("Matrix contents:n");
print_matrix(matrix, rows, cols);

for(int i = 0; i < rows; i++) {
free(matrix[i]);
}
free(matrix);

return 0;
}

## **6. Modify your program from question 5 to allow the user to specify a range for the random numbers (e.g., between -50 and 50).**

c
#include
#include
#include

void print_matrix(int **matrix, int rows, int cols) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("%dt", matrix[i][j]);
}
printf("n");
}
}

int main() {
int rows, cols, min, max;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
printf("Enter the minimum value: ");
scanf("%d", &min);
printf("Enter the maximum value: ");
scanf("%d", &max);

int **matrix = (int **) malloc(rows * sizeof(int *));

if(matrix == NULL) {
printf("Memory allocation failedn");
return 1;
}

for(int i = 0; i < rows; i++) {
matrix[i] = (int *) malloc(cols * sizeof(int));

if(matrix[i] == NULL) {
printf("Memory allocation failedn");
return 1;
}
}

srand(time(NULL));
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
matrix[i][j] = min + rand() % (max – min +1);
}
}

printf("Matrix contents:n");
print_matrix(matrix, rows, cols);

for(int i=0;i<rows;i++) {
free(matrix[i]);
}
free(matrix);

return 0;
}

## **7. Write a C program that uses dynamic memory allocation to implement a stack data structure. The program should provide functions for pushing and popping elements from the stack as well as checking if it is empty or full.**

c
#include
#include

typedef struct stack{
int top;
int capacity;
int* array;

}stack;

stack* create_stack(int size){
stack* s=malloc(sizeof(stack));
s->top=-1;
s->capacity=size;

s->array=malloc(s->capacity*sizeof(int));

return s;

}

void push(stack* s,int item){
if(s->top==s->capacity-1){
printf(“Stack Overflown”);
exit(EXIT_FAILURE);
}else{
s->array[++s->top]=item;
}
}

void pop(stack* s){
if(s->top==-1){
printf(“Stack Underflown”);
exit(EXIT_FAILURE);
}else{
s->top–;
}
}

void print_stack(stack* s){
if(s->top==-1){
printf(“nStack is Emptyn”);
}else{
for(int i=s->top;i>=0;i–){
printf(“%dn”,s->array[i]);
}
}
}

void peek(stack* s){
if(s->top==-1){
printf(“nStack is Emptyn”);
}else{
printf(“nThe top element is %dn”,s->array[s->top]);
}
}

void free_stack(stack* s){
free(s->array);
free(s);
}

int main(){
stack* s=create_stack(10);
push(s,10);
push(s,20);
push(s,30);
print_stack(s);
pop(s);
print_stack(s);
peek(s);
free_stack(s);

return EXIT_SUCCESS;

}

## **8. Write a C program that uses dynamic memory allocation to implement a queue data structure. The program should provide functions for enqueueing and dequeueing elements from the queue as well as checking if it is empty or full.**

c
#include
#include

typedef struct queue{
int front,rear,capacity;
int* array;

}queue;

queue* create_queue(int size){
queue* q=malloc(sizeof(queue));
q->front=q->rear=-1;
q->capacity=size;

q->array=malloc(q->capacity*sizeof(int));

return q;

}

void enqueue(queue* q,int item){
if(q->rear==q->capacity-1){
printf(“nQueue Overflown”);
exit(EXIT_FAILURE);
}else{
if(q->front==-1)
q->front++;
q->rear++;
q->array[q->rear]=item;
}
}

void dequeue(queue* q){
if(q->front==-1 || q->front>q->rear){
printf(“nQueue Underflown”);
exit(EXIT_FAILURE);
}else{
q->front++;
}
}

void print_queue(queue* q){
if(q->front==-1 || q->front>q->rear){
printf(“nQueue is Emptyn”);
}else{
for(int i=q->front;irear;i++){
printf(“%dt”,q->array[i]);
}
printf(“n”);
}
}

void free_queue(queue* q){
free(q->array);
free(q);
}

int main(){
queue* q=create_queue(10);
enqueue(q,10);enqueue(q,20);enqueue(q,30);print_queue(q);dequeue(q);print_queue(q);free_queue(q);

return EXIT_SUCCESS;

}

## **9. Write a C program that uses dynamic memory allocation to implement a linked list data structure. The program should provide functions for adding and removing elements from the list as well as searching for elements by their value or position in the list.**

c
#include
#include

typedef struct node{
int data;
struct node *next;

}node;

node *create_node(int item){
node *new_node=malloc(sizeof(node));
new_node->data=item;
new_node->next=NULL;

return new_node;

}

node *insert_at_beginning(node *head,int item){
node *new_node=create_node(item);
new_node->next=head;

return new_node;

}

node *insert_at_end(node *head,int item){
node *temp=head,*new_node=create_node(item);

while(temp!=NULL && temp->next!=NULL)
temp=temp->next;

temp=new_node;

return head;

}

node *delete_first(node *head,int item){
node *temp=head,*prev=NULL;

while(temp!=NULL && temp->data!=item){

prev=temp;
temp=temp->next;

}

if(prev==NULL)
head=temp ? temp -> next : NULL ;
else
prev -> next=temp ? temp -> next : NULL ;

return head ;

}

node *search_by_value(node *head,int item){

while(head!=NULL && head -> data != item)
head=head -> next ;

return head ;

}

node *search_by_position(node *head,int position){

int count=1;

while(head!=NULL && count next,count++ ;

return head ;

}

void free_list(node *head){

while(head!=NULL){

node *temp=head -> next ;
free(head) ;
head=temp ;

}

}

void print_list(node *head){

while(head!=NULL){

printf(“%dt”,head -> data ) ;
head=head -> next ;

}

printf(“n”) ;

}

int main(){

node *head=NULL,*temp=NULL;

head=insert_at_beginning(head,10);

head=insert_at_end(head,20);

head=insert_at_end(head,30);

print_list(head);

head=delete_first(head,20);

print_list(head);

temp=search_by_value(head,10);

if(temp==NULL)
printf(“nThe element not found in list n”) ;
else
printf(“nThe element found in list n”) ;

temp=search_by_position(head,2);

if(temp==NULL)
printf(“nThe element not found in list n”) ;
else
printf(“nThe element found in list n”) ;

free_list(head);

return EXIT_SUCCESS;

}

phuongdang94/Assigment-C data = new_data;
new_node -> next = (*head_ref);
(*head_ref) = new_node;
return (*head_ref);
}

**Source code**: [add_to_front.c](https://github.com/phuongdang94/Assignment-3-Linked-List/blob/master/add_to_front.c)

**Executable file**: [add_to_front.exe](https://github.com/phuongdang94/Assignment-3-Linked-List/blob/master/add_to_front.exe)

**Output**: [output_add_to_front.txt](https://github.com/phuongdang94/Assignment-3-Linked-List/blob/master/output_add_to_front.txt)

## Question-02

Write a C function `Node* add_to_end(Node** head_ref, int new_data)` that adds a new node with value `new_data` at the end of a singly linked list.

### Solution:

c
Node* add_to_end(Node** head_ref, int new_data)
{
Node* new_node = (Node*) malloc(sizeof(Node));
new_node -> data = new_data;
new_node -> next = NULL;

if (*head_ref == NULL) {
(*head_ref) = new_node ;
return (*head_ref);
}

Node* last = (*head_ref);

while(last -> next != NULL) {
last = last -> next ;
}

last -> next = new_node ;

return (*head_ref);
}

**Source code**: [add_to_end.c](https://github.com/phuongdang94/Assignment-3-Linked-List/blob/master/add_to_end.c)

**Executable file**: [add_to_end.exe](https://github.com/phuongdang94/Assignment-3-Linked-List/blob/master/add_to_end.exe)

**Output**: [output_add_to_end.txt](https://github.com/phuongdang94/Assignment-3-Linked-List/blob/master/output_add_to_end.txt)

## Question-03

Write a C function `Node* insert_after(Node** head_ref, Node* prev_node, int new_data)` that inserts a new node with value `new_data` after `prev_node` in a singly linked list.

### Solution:

c
Node* insert_after(Node** head_ref , Node* prev_node , int new_data)
{

if(prev_node == NULL){
fprintf(stderr , “the given previous node cannot be NULL “);
return NULL ;
}

Node* new_node =(Node*) malloc(sizeof(Node));

new_node -> data=new_data ;

new_node -> next=prev_node -> next ;

prev_node -> next=new_node ;

return (*head_ref);

}

**Source code**: [insert_after.c](https://github.com/phuongdang94/Assignment-3-Linked-List/blob/master/insert_after.c)

**Executable file**: [insert_after.exe](https://github.com/phuongdang94/Assignment-3-Linked-List/blob/master/insert_after.exe)

**Output**: [output_insert_after.txt](https://github.com/phuongdang94/Assignment-3-Linked-List/blob/master/output_insert_after.txt)

## Question-04

Write a C function `void delete_by_value(Node** head_ref , int key)` that deletes all nodes containing `key` from a singly linked list.

### Solution:

c
void delete_by_value(Node** head_ref , int key){

Node *temp=*head_ref,*prev=NULL ;

while(temp != NULL && temp -> data != key){
prev=temp ;
temp=temp -> next ;
}

if(prev == NULL){ //if key was at first node
(*head_ref)=temp -> next ;
} else {
prev -> next=temp -> next ;
}

free(temp); //freeing deleted node

}

**Source code**: [delete_by_value.c](https://github.com/phuongdang94/Assignment-3-Linked-List/blob/master/delete_by_value.c)

**Executable file**: [delete_by_value.exe](https://github.com/phuongdang94/Assignment-3-Linked-List/blob/master/delete_by_value.exe)

**Output**: [output_delete_by_value.txt](https://github.com/phuongdang94/Assignment-3-Linked-List/blob/master/output_delete_by_value.txt)

## Question-05

Write a C function `void reverse_list(Node** head_ref)` that reverses a singly linked list.

### Solution:

c
void reverse_list(Node** head_ref){

Node *prev=NULL,*current=*head_ref,*next=NULL ;

while(current != NULL){
next=current -> next ;
current -> next=prev ;
prev=current ;
current=next ;
}

(*head_ref)=prev ;
}

**Source code**: [reverse_list.c](https://github.com/phuongdang94/Assignment-3-Linked-List/blob/master/reverse_list.c)

**Executable file**: [reverse_list.exe](https://github.com/phuongdang94/Assignment-3<|repo

150% até R$ 1.500 - Primeiro depósito
100% até R$ 1.000 - Para iniciantes
200% até R$ 2.000 - Pacote premium