Programming questions assignment
question1.dev
[Project] FileName=question1.dev Name=question1 Type=1 Ver=2 ObjFiles= Includes= Libs= PrivateResource= ResourceIncludes= MakeIncludes= Compiler= CppCompiler= Linker= IsCpp=1 Icon= ExeOutput= ObjectOutput= LogOutput= LogOutputEnabled=0 OverrideOutput=0 OverrideOutputName= HostApplication= UseCustomMakefile=0 CustomMakefile= CommandLine= Folders= IncludeVersionInfo=0 SupportXPThemes=0 CompilerSet=0 CompilerSettings=0000000000000000000000000 UnitCount=1 [VersionInfo] Major=1 Minor=0 Release=0 Build=0 LanguageID=1033 CharsetID=1252 CompanyName= FileVersion= FileDescription=Developed using the Dev-C++ IDE InternalName= LegalCopyright= LegalTrademarks= OriginalFilename= ProductName= ProductVersion= AutoIncBuildNr=0 SyncProduct=1 [Unit1] FileName=qxn1.cpp CompileCpp=1 Folder= Compile=1 Link=1 Priority=1000 OverrideBuildCmd=0 BuildCmd=
qxn4b.cpp
qxn4b.cpp
//Merge-sort is based on the divide-and-conquer paradigm. It involves the following three steps:
// Divide the array into two (or more) subarrays
// Sort each subarray (Conquer)
//Merge them into one (in a smart way!)
#include
<
stdio
.
h
>
void
merge_sort
(
long
[],
long
);
int
main
()
{
long
array
[
100
],
n
,
c
,
d
,
swap
;
printf
(
"Enter number of elements\n"
);
scanf
(
"%ld"
,
&
n
);
printf
(
"Enter %ld integers\n"
,
n
);
for
(
c
=
0
;
c
<
n
;
c
++
)
scanf
(
"%ld"
,
&
array
[
c
]);
merge_sort
(
array
,
n
);
printf
(
"Sorted list in ascending order:\n"
);
for
(
c
=
0
;
c
<
n
;
c
++
)
printf
(
"%ld\n"
,
array
[
c
]);
return
0
;
}
void
merge_sort
(
long
list
[],
long
n
)
{
long
c
,
d
,
t
;
for
(
c
=
0
;
c
<
(
n
-
1
);
c
++
)
{
for
(
d
=
0
;
d
<
n
-
c
-
1
;
d
++
)
{
if
(
list
[
d
]
>
list
[
d
+
1
])
{
/* Swapping */
t
=
list
[
d
];
list
[
d
]
=
list
[
d
+
1
];
list
[
d
+
1
]
=
t
;
}
}
}
}
qxn4a.cpp
qxn4a.cpp
#include
<
stdio
.
h
>
void
bubble_sort
(
long
[],
long
);
int
main
()
{
long
array
[
100
],
n
,
c
,
d
,
swap
;
printf
(
"Enter number of elements\n"
);
scanf
(
"%ld"
,
&
n
);
printf
(
"Enter %ld integers\n"
,
n
);
for
(
c
=
0
;
c
<
n
;
c
++
)
scanf
(
"%ld"
,
&
array
[
c
]);
bubble_sort
(
array
,
n
);
printf
(
"Sorted list in ascending order:\n"
);
for
(
c
=
0
;
c
<
n
;
c
++
)
printf
(
"%ld\n"
,
array
[
c
]);
return
0
;
}
void
bubble_sort
(
long
list
[],
long
n
)
{
long
c
,
d
,
t
;
for
(
c
=
0
;
c
<
(
n
-
1
);
c
++
)
{
for
(
d
=
0
;
d
<
n
-
c
-
1
;
d
++
)
{
if
(
list
[
d
]
>
list
[
d
+
1
])
{
/* Swapping */
t
=
list
[
d
];
list
[
d
]
=
list
[
d
+
1
];
list
[
d
+
1
]
=
t
;
}
}
}
}
qxn3b.cpp
qxn3b.cpp
#include
<
stdio
.
h
>
#include
<
stdlib
.
h
>
// list_node structure
typedef
struct
list_node
{
int
item
;
struct
list_node
*
next
;
}
ListNode
;
//call functions that will be used
void
printNode
(
ListNode
*
head
);
int
removeNode
(
ListNode
**
ptrhead
,
int
index
);
ListNode
*
findNode
(
ListNode
*
head
,
int
index
);
int
main
(){
int
index
,
value
;
ListNode
*
head
=
NULL
;
ListNode
*
temp
;
//build the list
printf
(
"Enter a value:"
);
scanf
(
"%d"
,
&
value
);
do
{
temp
->
item
=
value
;
if
(
head
==
NULL
){
head
=
(
struct
list_node
*
)
malloc
(
sizeof
(
ListNode
));
temp
=
head
;
}
else
{
temp
->
next
=
(
struct
list_node
*
)
malloc
(
sizeof
(
ListNode
));
temp
=
temp
->
next
;
}
printf
(
"Enter a value:"
);
scanf
(
"%d"
,
&
value
);
}
while
(
value
!=-
1
);
printf
(
"Enter the index: "
);
scanf
(
"%d"
,
&
index
);
// remove the node at the position indexed
// when I used debugger, I saw it didn't execute this step. Maybe there's something wrong with it....
removeNode
(
&
head
,
index
);
printNode
(
head
);
return
0
;
}
void
printNode
(
ListNode
*
head
){
if
(
head
==
NULL
)
exit
(
0
);
while
(
head
!=
NULL
){
printf
(
"%d"
,
head
->
item
);
head
=
head
->
next
;
}
printf
(
"\n"
);
}
ListNode
*
findNode
(
ListNode
*
head
,
int
index
){
if
(
head
==
NULL
||
index
<
0
)
return
NULL
;
while
(
index
>
0
){
head
=
head
->
next
;
index
--
;
}
return
head
;
}
int
removeNode
(
ListNode
**
ptrhead
,
int
index
){
ListNode
*
pre
,
*
cur
,
*
temphead
;
temphead
=*
ptrhead
;
if
(
findNode
(
temphead
,
index
)
!=
NULL
){
pre
=
findNode
(
temphead
,
index
);
cur
=
pre
->
next
;
temphead
->
next
=
cur
;
return
0
;
}
else
return
-
1
;
}
qxn3a.cpp
#include <stdio.h> #include <stdlib.h> /* structure of a linked list node */ struct node { int data; struct node *next; }; void deleteNode(struct node *head, struct node *n) { // When node to be deleted is head node if(head == n) { if(head->next == NULL) { printf("There is only one node. The list can't be made empty "); return; } /* Copy the data of next node to head */ head->data = head->next->data; // store address of next node n = head->next; // Remove the link of next node head->next = head->next->next; // free memory free(n); return; } // When not first node, follow the normal deletion process // find the previous node struct node *prev = head; while(prev->next != NULL && prev->next != n) prev = prev->next; // Check if node really exists in Linked List if(prev->next == NULL) { printf("\n Given node is not present in Linked List"); return; } // Remove node from Linked List prev->next = prev->next->next; // Free memory free(n); return; } /* Utility function to insert a node at the begining */ void push(struct node **head_ref, int new_data) { struct node *new_node = (struct node *)malloc(sizeof(struct node)); new_node->data = new_data; new_node->next = *head_ref; *head_ref = new_node; } /* Utility function to print a linked list */ void printList(struct node *head) { while(head!=NULL) { printf("%d ",head->data); head=head->next; } printf("\n"); } /* Driver program to test above functions */ int main() { struct node *head = NULL; /* Create following linked list 12->15->10->11->5->6->2->3 */ push(&head,3); push(&head,2); push(&head,6); push(&head,5); push(&head,11); push(&head,10); push(&head,15); push(&head,12); printf("Given Linked List: "); printList(head); /* Let us delete the node with value 10 */ printf("\nDeleting node %d: ", head->next->next->data); deleteNode(head, head->next->next); printf("\nModified Linked List: "); printList(head); /* Let us delete the the first node */ printf("\nDeleting first node "); deleteNode(head, head); printf("\nModified Linked List: "); printList(head); getchar(); return 0; }
qxn2b.cpp
qxn2b.cpp
/* C Program to remove duplicates from a sorted linked list */
#include
<
stdio
.
h
>
#include
<
stdlib
.
h
>
/* Link list node */
struct
node
{
int
data
;
struct
node
*
next
;
};
/* The function removes duplicates from a sorted list */
void
removeDuplicates
(
struct
node
*
head
)
{
/* Pointer to traverse the linked list */
struct
node
*
current
=
head
;
/* Pointer to store the next pointer of a node to be deleted*/
struct
node
*
next_next
;
/* do nothing if the list is empty */
if
(
current
==
NULL
)
return
;
/* Traverse the list till last node */
while
(
current
->
next
!=
NULL
)
{
/* Compare current node with next node */
if
(
current
->
data
==
current
->
next
->
data
)
{
/* The sequence of steps is important*/
next_next
=
current
->
next
->
next
;
free
(
current
->
next
);
current
->
next
=
next_next
;
}
else
/* This is tricky: only advance if no deletion */
{
current
=
current
->
next
;
}
}
}
/* UTILITY FUNCTIONS */
/* Function to insert a node at the beginging of the linked list */
void
push
(
struct
node
**
head_ref
,
int
new_data
)
{
/* allocate node */
struct
node
*
new_node
=
(
struct
node
*
)
malloc
(
sizeof
(
struct
node
));
/* put in the data */
new_node
->
data
=
new_data
;
/* link the old list off the new node */
new_node
->
next
=
(
*
head_ref
);
/* move the head to point to the new node */
(
*
head_ref
)
=
new_node
;
}
/* Function to print nodes in a given linked list */
void
printList
(
struct
node
*
node
)
{
while
(
node
!=
NULL
)
{
printf
(
"%d "
,
node
->
data
);
node
=
node
->
next
;
}
}
/* Drier program to test above functions*/
int
main
()
{
/* Start with the empty list */
struct
node
*
head
=
NULL
;
/* Let us create a sorted linked list to test the functions
Created linked list will be 11->11->11->13->13->20 */
push
(
&
head
,
20
);
push
(
&
head
,
13
);
push
(
&
head
,
13
);
push
(
&
head
,
11
);
push
(
&
head
,
11
);
push
(
&
head
,
11
);
printf
(
"\n Linked list before duplicate removal "
);
printList
(
head
);
/* Remove duplicates from linked list */
removeDuplicates
(
head
);
printf
(
"\n Linked list after duplicate removal "
);
printList
(
head
);
return
0
;
}
question4b.dev
[Project] FileName=question4b.dev Name=question4b Type=1 Ver=2 ObjFiles= Includes= Libs= PrivateResource= ResourceIncludes= MakeIncludes= Compiler= CppCompiler= Linker= IsCpp=1 Icon= ExeOutput= ObjectOutput= LogOutput= LogOutputEnabled=0 OverrideOutput=0 OverrideOutputName= HostApplication= UseCustomMakefile=0 CustomMakefile= CommandLine= Folders= IncludeVersionInfo=0 SupportXPThemes=0 CompilerSet=0 CompilerSettings=0000000000000000000000000 UnitCount=1 [VersionInfo] Major=1 Minor=0 Release=0 Build=0 LanguageID=1033 CharsetID=1252 CompanyName= FileVersion= FileDescription=Developed using the Dev-C++ IDE InternalName= LegalCopyright= LegalTrademarks= OriginalFilename= ProductName= ProductVersion= AutoIncBuildNr=0 SyncProduct=1 [Unit1] FileName=qxn4b.cpp CompileCpp=1 Folder= Compile=1 Link=1 Priority=1000 OverrideBuildCmd=0 BuildCmd=
question4a.dev
[Project] FileName=question4a.dev Name=question4a Type=1 Ver=2 ObjFiles= Includes= Libs= PrivateResource= ResourceIncludes= MakeIncludes= Compiler= CppCompiler= Linker= IsCpp=1 Icon= ExeOutput= ObjectOutput= LogOutput= LogOutputEnabled=0 OverrideOutput=0 OverrideOutputName= HostApplication= UseCustomMakefile=0 CustomMakefile= CommandLine= Folders= IncludeVersionInfo=0 SupportXPThemes=0 CompilerSet=0 CompilerSettings=0000000000000000000000000 UnitCount=1 [VersionInfo] Major=1 Minor=0 Release=0 Build=0 LanguageID=1033 CharsetID=1252 CompanyName= FileVersion= FileDescription=Developed using the Dev-C++ IDE InternalName= LegalCopyright= LegalTrademarks= OriginalFilename= ProductName= ProductVersion= AutoIncBuildNr=0 SyncProduct=1 [Unit1] FileName=qxn4a.cpp CompileCpp=1 Folder= Compile=1 Link=1 Priority=1000 OverrideBuildCmd=0 BuildCmd=
question3b.dev
[Project] FileName=question3b.dev Name=question3b Type=1 Ver=2 ObjFiles= Includes= Libs= PrivateResource= ResourceIncludes= MakeIncludes= Compiler= CppCompiler= Linker= IsCpp=1 Icon= ExeOutput= ObjectOutput= LogOutput= LogOutputEnabled=0 OverrideOutput=0 OverrideOutputName= HostApplication= UseCustomMakefile=0 CustomMakefile= CommandLine= Folders= IncludeVersionInfo=0 SupportXPThemes=0 CompilerSet=0 CompilerSettings=0000000000000000000000000 UnitCount=1 [VersionInfo] Major=1 Minor=0 Release=0 Build=0 LanguageID=1033 CharsetID=1252 CompanyName= FileVersion= FileDescription=Developed using the Dev-C++ IDE InternalName= LegalCopyright= LegalTrademarks= OriginalFilename= ProductName= ProductVersion= AutoIncBuildNr=0 SyncProduct=1 [Unit1] FileName=qxn3b.cpp CompileCpp=1 Folder= Compile=1 Link=1 Priority=1000 OverrideBuildCmd=0 BuildCmd=
question3a.dev
[Project] FileName=question3a.dev Name=question3a Type=1 Ver=2 ObjFiles= Includes= Libs= PrivateResource= ResourceIncludes= MakeIncludes= Compiler= CppCompiler= Linker= IsCpp=1 Icon= ExeOutput= ObjectOutput= LogOutput= LogOutputEnabled=0 OverrideOutput=0 OverrideOutputName= HostApplication= UseCustomMakefile=0 CustomMakefile= CommandLine= Folders= IncludeVersionInfo=0 SupportXPThemes=0 CompilerSet=0 CompilerSettings=0000000000000000000000000 UnitCount=1 [VersionInfo] Major=1 Minor=0 Release=0 Build=0 LanguageID=1033 CharsetID=1252 CompanyName= FileVersion= FileDescription=Developed using the Dev-C++ IDE InternalName= LegalCopyright= LegalTrademarks= OriginalFilename= ProductName= ProductVersion= AutoIncBuildNr=0 SyncProduct=1 [Unit1] FileName=qxn3a.cpp CompileCpp=1 Folder= Compile=1 Link=1 Priority=1000 OverrideBuildCmd=0 BuildCmd=
question2b.dev
[Project] FileName=question2b.dev Name=question2b Type=1 Ver=2 ObjFiles= Includes= Libs= PrivateResource= ResourceIncludes= MakeIncludes= Compiler= CppCompiler= Linker= IsCpp=1 Icon= ExeOutput= ObjectOutput= LogOutput= LogOutputEnabled=0 OverrideOutput=0 OverrideOutputName= HostApplication= UseCustomMakefile=0 CustomMakefile= CommandLine= Folders= IncludeVersionInfo=0 SupportXPThemes=0 CompilerSet=0 CompilerSettings=0000000000000000000000000 UnitCount=1 [VersionInfo] Major=1 Minor=0 Release=0 Build=0 LanguageID=1033 CharsetID=1252 CompanyName= FileVersion= FileDescription=Developed using the Dev-C++ IDE InternalName= LegalCopyright= LegalTrademarks= OriginalFilename= ProductName= ProductVersion= AutoIncBuildNr=0 SyncProduct=1 [Unit1] FileName=qxn2b.cpp CompileCpp=1 Folder= Compile=1 Link=1 Priority=1000 OverrideBuildCmd=0 BuildCmd=
question2a.dev
[Project] FileName=question2a.dev Name=question2a Type=1 Ver=2 ObjFiles= Includes= Libs= PrivateResource= ResourceIncludes= MakeIncludes= Compiler= CppCompiler= Linker= IsCpp=1 Icon= ExeOutput= ObjectOutput= LogOutput= LogOutputEnabled=0 OverrideOutput=0 OverrideOutputName= HostApplication= UseCustomMakefile=0 CustomMakefile= CommandLine= Folders= IncludeVersionInfo=0 SupportXPThemes=0 CompilerSet=0 CompilerSettings=0000000000000000000000000 UnitCount=1 [VersionInfo] Major=1 Minor=0 Release=0 Build=0 LanguageID=1033 CharsetID=1252 CompanyName= FileVersion= FileDescription=Developed using the Dev-C++ IDE InternalName= LegalCopyright= LegalTrademarks= OriginalFilename= ProductName= ProductVersion= AutoIncBuildNr=0 SyncProduct=1 [Unit1] FileName=question2a.cpp CompileCpp=1 Folder= Compile=1 Link=1 Priority=1000 OverrideBuildCmd=0 BuildCmd=
question2a.cpp
question2a.cpp
/* C/C++ program to merge two sorted linked lists */
#include
<
stdio
.
h
>
#include
<
stdlib
.
h
>
#include
<
assert
.
h
>
/* Link list node */
struct
node
{
int
data
;
struct
node
*
next
;
};
/* pull off the front node of the source and put it in dest */
void
MoveNode
(
struct
node
**
destRef
,
struct
node
**
sourceRef
);
/* Takes two lists sorted in increasing order, and splices
their nodes together to make one big sorted list which
is returned. */
struct
node
*
SortedMerge
(
struct
node
*
a
,
struct
node
*
b
)
{
/* a dummy first node to hang the result on */
struct
node dummy
;
/* tail points to the last result node */
struct
node
*
tail
=
&
dummy
;
/* so tail->next is the place to add new nodes
to the result. */
dummy
.
next
=
NULL
;
while
(
1
)
{
if
(
a
==
NULL
)
{
/* if either list runs out, use the
other list */
tail
->
next
=
b
;
break
;
}
else
if
(
b
==
NULL
)
{
tail
->
next
=
a
;
break
;
}
if
(
a
->
data
<=
b
->
data
)
MoveNode
(
&
(
tail
->
next
),
&
a
);
else
MoveNode
(
&
(
tail
->
next
),
&
b
);
tail
=
tail
->
next
;
}
return
(
dummy
.
next
);
}
/* UTILITY FUNCTIONS */
/* MoveNode() function takes the node from the front of the
source, and move it to the front of the dest.
It is an error to call this with the source list empty.
Before calling MoveNode():
source == {1, 2, 3}
dest == {1, 2, 3}
Affter calling MoveNode():
source == {2, 3}
dest == {1, 1, 2, 3} */
void
MoveNode
(
struct
node
**
destRef
,
struct
node
**
sourceRef
)
{
/* the front source node */
struct
node
*
newNode
=
*
sourceRef
;
assert
(
newNode
!=
NULL
);
/* Advance the source pointer */
*
sourceRef
=
newNode
->
next
;
/* Link the old dest off the new node */
newNode
->
next
=
*
destRef
;
/* Move dest to point to the new node */
*
destRef
=
newNode
;
}
/* Function to insert a node at the beginging of the
linked list */
void
push
(
struct
node
**
head_ref
,
int
new_data
)
{
/* allocate node */
struct
node
*
new_node
=
(
struct
node
*
)
malloc
(
sizeof
(
struct
node
));
/* put in the data */
new_node
->
data
=
new_data
;
/* link the old list off the new node */
new_node
->
next
=
(
*
head_ref
);
/* move the head to point to the new node */
(
*
head_ref
)
=
new_node
;
}
/* Function to print nodes in a given linked list */
void
printList
(
struct
node
*
node
)
{
while
(
node
!=
NULL
)
{
printf
(
"%d "
,
node
->
data
);
node
=
node
->
next
;
}
}
/* Drier program to test above functions*/
int
main
()
{
/* Start with the empty list */
struct
node
*
res
=
NULL
;
struct
node
*
a
=
NULL
;
struct
node
*
b
=
NULL
;
/* Let us create two sorted linked lists to test
the functions
Created lists, a: 5->10->15, b: 2->3->20 */
push
(
&
a
,
15
);
push
(
&
a
,
10
);
push
(
&
a
,
5
);
push
(
&
b
,
20
);
push
(
&
b
,
3
);
push
(
&
b
,
2
);
/* Remove duplicates from linked list */
res
=
SortedMerge
(
a
,
b
);
printf
(
"Merged Linked List is: \n"
);
printList
(
res
);
return
0
;
}
qxn1.cpp
#include <stdio.h> int a[] = {1}; int b[] = {1, 2, 3}; int c[] = {1, 2, 3, 4, 5}; int d[] = {1, 2, 3, 4, 5, 6, 7}; int e[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int f[] = {1, 2, 3, 4, 5, 6, 7}; int g[] = {1, 2, 3, 4, 5}; int h[] = {1, 2, 3}; int j[] = {1}; int sumnum(int [], int); printf("enter row you want to get the sum: \int") int main() { printf("%d\n", sumnum(e, 9)); return 0; }