Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions algorithms/Search/binary-search.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdio.h>
//#include <conio.h>

void main() {
int query,i,halfIndex,fullIndex;
int arr[6] = {11,23,35,56,67,78};

//clrscr();
printf("\n Enter element to search:");
scanf("%d",&query);

halfIndex = sizeof(arr)/2;
fullIndex = sizeof(arr)-1;

if (query == arr[halfIndex]){
printf("\n Element at %d \n",halfIndex);
}
else if (query < arr[halfIndex]){
for (i=0;i<arr[halfIndex];i++){
if (query == arr[i]){
printf("\n Element at %d \n",i);
}
}
}
else if (query > arr[halfIndex]){
for (i=arr[halfIndex];i< fullIndex;i++){
if (query == arr[i]){
printf("\n Element at %d \n",i);
}
}
}
else {
printf("Element not in array");
}
//getch();
}
27 changes: 27 additions & 0 deletions algorithms/Search/linear-search.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdio.h>
//#include <conio.h>

void main() {
int query,i,finalIndex,found=0;
int arr[5] = {11,66,44,22,55};
//clrscr();

printf("Program for linear search\n");
printf("Enter element to search:");
scanf("%d",&query);

finalIndex = sizeof(arr) - 1;

for (i=0;i<=finalIndex;i++){ //checks for query at every iteration
if (arr[i]==query){
printf("Element at %d index\n",i);
found = 1;
break;
}
}

if (found == 0){
printf("Element not in list\n");
}
//getch();
}
55 changes: 55 additions & 0 deletions data-structures/linear-ds/matrices/matrix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <stdio.h>
// #include <conio.h>
#include <stdlib.h>

void main() {
int row, column;
int opt, element;
int i=0, j=0;
printf("\n PROGRAM FOR MATRIX");

printf("\n Enter size of the matrix:");
printf("\n Rows:");
scanf("%d",&row);
printf("\n Columns:");
scanf("%d",&column);

int matrix[row][column];

printf("\n MATRIX OF SIZE %d X %d CREATED \n", row, column);

while(1) {
printf("\n MENU");
printf("\n 1. Add elements to matrix");
printf("\n 2. Show matrix");
printf("\n 3. Exit");
printf("\n Enter your choice:");
scanf("%d",&opt);

switch(opt){
case(1):
for (i=0; i<row; i++){
for (j=0;j<column; j++){
printf("\n Enter element to be inserted at [%d][%d]:", i, j);
scanf("%d",&element);
matrix[i][j] = element;
}
}
break;

case(2):
for (i=0; i<row; i++){
for (j=0;j<column; j++){
printf("\t %d \t",matrix[i][j]);
}
printf("\n");
}
break;

case(3):
exit(0);

}
}
// getch();
}
57 changes: 57 additions & 0 deletions data-structures/linear-ds/matrices/sparse-martix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <stdio.h>
//#include <conio.h>

void main() {
int m,n,i,j,index=0;

struct nonzero {
int row;
int column;
int element;
} nonzero[100]; //list of nonzero element as structure

printf("\nPROGRAM FOR SPARSE MATRIX AND RETURING NON ZERO ELEMENTS WITH POSITIONS\n");
printf("Enter number of row in matrix:");
scanf("%d",&m);
printf("\nEnter number of columns in matrix:");
scanf("%d",&n);

int sparse[m][n]; //sparse matrix

//clrscr();
// pushing elements
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("Enter element at [%d][%d]:",i,j);
scanf("%d",&sparse[i][j]);
}
}

//displaying matrix
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d\t",sparse[i][j]);
}
printf("\n");
}

//finding non zero elements
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if (sparse[i][j] != 0){
nonzero[index].row = i;
nonzero[index].column = j;
nonzero[index].element = sparse[i][j];
index++;
}
}
}

// displaying non zero elements alongwith their location
for (i=0;i<index;i++){
printf("Element %d at [%d][%d]\n",nonzero[i].element,
nonzero[i].row,nonzero[i].column);
}
//getch();

}