Skip to content

Commit

Permalink
code cleanup to prevent gcc warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
kvedala committed Apr 8, 2020
1 parent 64789ae commit 1b82680
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 196 deletions.
129 changes: 67 additions & 62 deletions client_server/client.c
Original file line number Diff line number Diff line change
@@ -1,66 +1,71 @@

// Write CPP code here
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
// Write CPP code here
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
}

int main()
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;

// socket create and varification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for (;;)
{
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0)
{
printf("Client Exit...\n");
break;
}
}
}

int main()
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;

// socket create and varification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));

// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);

// connect the client socket to server socket
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
printf("connection with the server failed...\n");
exit(0);
}
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));

// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);

// connect the client socket to server socket
if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) != 0)
{
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");
// function for chat
func(sockfd);
// close the socket
close(sockfd);
}
printf("connected to the server..\n");

// function for chat
func(sockfd);

// close the socket
close(sockfd);
}
15 changes: 8 additions & 7 deletions conversions/decimal_to_octal_recursion.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@
#include <stdio.h>
int decimal_to_octal(int decimal)
{
if( (decimal<8) && (decimal>0) )
if ((decimal < 8) && (decimal > 0))
{
return decimal;
}
else if(decimal==0)
else if (decimal == 0)
{
return 0;
}
else
{
return ( (decimal_to_octal(decimal/8)*10) + decimal%8 );
return ((decimal_to_octal(decimal / 8) * 10) + decimal % 8);
}
}
void main()
int main()
{
int octalNumber,decimalNumber;
int octalNumber, decimalNumber;
printf("\nEnter your decimal number : ");
scanf("%d",&decimalNumber);
scanf("%d", &decimalNumber);
octalNumber = decimal_to_octal(decimalNumber);
printf("\nThe octal of %d is : %d" ,decimalNumber,octalNumber);
printf("\nThe octal of %d is : %d", decimalNumber, octalNumber);
return 0;
}
101 changes: 62 additions & 39 deletions misc/rselect.c
Original file line number Diff line number Diff line change
@@ -1,54 +1,77 @@
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
void swap(int *a ,int *b)
{int t;t =*a;*a=*b;*b=t;}
int part(int a[],int l,int r,int n,int pivot,int pindex)
{int p1=l,p2=r;
while(p2>p1)
#include <stdlib.h>
#include <time.h>
void swap(int *a, int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}
int part(int a[], int l, int r, int n, int pivot, int pindex)
{
int p1 = l, p2 = r;
while (p2 > p1)
{
if (a[p1] > pivot && a[p2]<pivot)
{swap(&a[p1],&a[p2]);}
if (a[p1] > pivot && a[p2] < pivot)
{
swap(&a[p1], &a[p2]);
}
else
{
if (a[p1] <=pivot)
{p1++;}
if (a[p2]>=pivot)
{p2--;}
if (a[p1] <= pivot)
{
p1++;
}
if (a[p2] >= pivot)
{
p2--;
}
}
}
swap(&a[pindex],&a[p2]);
swap(&a[pindex], &a[p2]);
return p2;
}
int rselect(int a[],int l,int r,int n,int o)
int rselect(int a[], int l, int r, int n, int o)
{
int pivot,pindex,pactual;
if (r>l)
int pivot, pindex, pactual;
if (r > l)
{
pindex = rand() % (r - l + 1);
pivot = a[pindex];
pactual = part(a, l, r, n, pivot, pindex);

if (pactual == o)
{
return a[pactual];
}

if (o < pactual)
{
rselect(a, l, pactual - 1, n, o);
}

if (o > pactual)
{
rselect(a, pactual + 1, r, n, o - pactual);
}
}
if (r == l)
{
pindex = rand()%(r-l+1);
pivot = a[pindex];
pactual = part(a,l,r,n,pivot,pindex);

if (pactual == o)
{return a[pactual];}

if (o < pactual)
{rselect(a,l,pactual-1,n,o);}

if (o>pactual)
{rselect(a,pactual+1,r,n,o-pactual);}
return a[l];
}
if (r==l)
{return a[l];}
return -1;
}
int main()
{srand(time(NULL));
int n,o,i,*a;
scanf("%d %d",&n,&o);
a = (int*)malloc(n*sizeof(int));
for (i=0;i<n;i++)
{scanf("%d",a+i);}
printf("\n\n%d",rselect(a,0,n-1,n,o));
{
srand(time(NULL));
int n, o, i, *a;
scanf("%d %d", &n, &o);
a = (int *)malloc(n * sizeof(int));
for (i = 0; i < n; i++)
{
scanf("%d", a + i);
}
printf("\n\n%d", rselect(a, 0, n - 1, n, o));
return 0;
}

36 changes: 18 additions & 18 deletions misc/strong_Number.c
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
/**
* Modified on 07/12/2017, Kyler Smith
*
* A number is called strong number if sum of the
* A number is called strong number if sum of the
* factorial of its digit is equal to number itself.
*/

#include<stdio.h>

#include <stdio.h>

void strng(int a)
{
int j=a;
int sum=0;
int b,i,fact=1;
while(a>0)
int j = a;
int sum = 0;
int b, i, fact = 1;
while (a > 0)
{
fact=1;
b=a%10;
for(i=1;i<=b;i++)
fact = 1;
b = a % 10;
for (i = 1; i <= b; i++)
{
fact=fact*i;
fact = fact * i;
}
a=a/10;
sum=sum+fact;
a = a / 10;
sum = sum + fact;
}
if(sum==j)
printf("%d is a strong number",j);
if (sum == j)
printf("%d is a strong number", j);
else
printf("%d is not a strong number",j);
printf("%d is not a strong number", j);
}
void main()
int main()
{
int a;
printf("Enter the number to check");
scanf("%d",&a);
scanf("%d", &a);
strng(a);
return 0;
}
Loading

0 comments on commit 1b82680

Please sign in to comment.