forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
code cleanup to prevent gcc warnings
- Loading branch information
Showing
11 changed files
with
244 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.