code cleanup to prevent gcc warnings

This commit is contained in:
Krishna Vedala 2020-04-08 09:41:12 -04:00
parent 64789aed99
commit 1b826807ed
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7
11 changed files with 244 additions and 196 deletions

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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);}
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)
{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;
}

View File

@ -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;
}

View File

@ -6,47 +6,61 @@ e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
#include <stdio.h>
#include <math.h>
int isprime(int no) {
int isprime(int no)
{
int sq;
if (no == 2) {
if (no == 2)
{
return 1;
}
else if (no%2 == 0) {
else if (no % 2 == 0)
{
return 0;
}
sq = ((int)(sqrt(no))) + 1;
for (int i = 3; i < sq; i + 2) {
if (no%i == 0) {
for (int i = 3; i < sq; i += 2)
{
if (no % i == 0)
{
return 0;
}
}
return 1;
}
int main() {
int main()
{
int maxNumber = 0;
int n = 0;
int n1;
scanf("%d", &n);
if (isprime(n) == 1)
printf("%d", n);
else {
while (n % 2 == 0) {
else
{
while (n % 2 == 0)
{
n = n / 2;
}
if (isprime(n) == 1) {
if (isprime(n) == 1)
{
printf("%d\n", n);
}
else {
else
{
n1 = ((int)(sqrt(n))) + 1;
for (int i = 3; i < n1; i + 2) {
if (n%i == 0) {
if (isprime((int)(n / i)) == 1) {
for (int i = 3; i < n1; i += 2)
{
if (n % i == 0)
{
if (isprime((int)(n / i)) == 1)
{
maxNumber = n / i;
break;
}
else if (isprime(i) == 1) {
else if (isprime(i) == 1)
{
maxNumber = i;
}
}

View File

@ -1,28 +1,34 @@
#include <stdio.h>
unsigned long gcd(unsigned long a, unsigned long b) {
unsigned long gcd(unsigned long a, unsigned long b)
{
unsigned long r;
if (a > b) {
if (a > b)
{
unsigned long t = a;
a = b;
b = t;
}
while (r = a % b) {
while ((r = (a % b)))
{
a = b;
b = r;
}
return b;
}
unsigned long lcm(unsigned long a, unsigned long b) {
unsigned long lcm(unsigned long a, unsigned long b)
{
unsigned long long p = (unsigned long long)a * b;
return p / gcd(a, b);
}
int main(void) {
int main(void)
{
unsigned long ans = 1;
unsigned long i;
for (i = 1; i <= 20; i++) {
for (i = 1; i <= 20; i++)
{
ans = lcm(ans, i);
}
printf("%lu\n", ans);

View File

@ -2,27 +2,24 @@
#include <stdlib.h>
#ifdef _OPENMP
#include <omp.h>
#pragma message ("Using OpenMP parallelization")
#else
#pragma message ("Not using OpenMP parallelization")
#include <omp.h>
#endif
/**
* Computes the length of collatz sequence for a given
* starting number
* Computes the length of collatz sequence for a given
* starting number
**/
long long collatz(long long start_num)
{
long long length = 1;
while (start_num != 1) /* loop till we reach 1 */
while (start_num != 1) /* loop till we reach 1 */
{
if(start_num & 0x01) /* check for odd */
if (start_num & 0x01) /* check for odd */
start_num = 3 * start_num + 1;
else
start_num >>= 1; /* simpler divide by 2 */
length ++;
start_num >>= 1; /* simpler divide by 2 */
length++;
}
return length;
@ -33,39 +30,39 @@ int main(int argc, char **argv)
long long max_len = 0, max_len_num = 0;
long long MAX_NUM = 1000000;
if (argc == 2) /* set commandline argumnet as the maximum iteration number */
if (argc == 2) /* set commandline argumnet as the maximum iteration number */
{
MAX_NUM = atoll(argv[1]);
printf("Maximum number: %lld\n", MAX_NUM);
}
/**
* Since the computational values for each iteration step are independent,
* we can compute them in parallel. However, the maximum values should be
/**
* Since the computational values for each iteration step are independent,
* we can compute them in parallel. However, the maximum values should be
* updated in synchrony so that we do not get into a "race condition".
*
*
* To compile with supporintg gcc or clang, the flag "-fopenmp" should be passes
* while with Microsoft C compiler, the flag "/fopenmp" should be used.
*
*
* Automatically detects for OPENMP using the _OPENMP macro.
**/
#ifdef _OPENMP
#pragma omp parallel for shared(max_len, max_len_num) schedule(guided)
#endif
#ifdef _OPENMP
#pragma omp parallel for shared(max_len, max_len_num) schedule(guided)
#endif
for (long long i = 1; i < MAX_NUM; i++)
{
long long L = collatz(i);
if (L > max_len)
{
max_len = L; /* length of sequence */
max_len_num = i; /* starting number */
max_len = L; /* length of sequence */
max_len_num = i; /* starting number */
}
#if defined(_OPENMP) && defined(DEBUG)
#if defined(_OPENMP) && defined(DEBUG)
printf("Thread: %2d\t %3lld: \t%5lld\n", omp_get_thread_num(), i, L);
#elif defined(DEBUG)
#elif defined(DEBUG)
printf("%3lld: \t%5lld\n", i, L);
#endif
#endif
}
printf("Start: %3lld: \tLength: %5lld\n", max_len_num, max_len);

View File

@ -1,27 +1,32 @@
#include <stdio.h>
int linearsearch(int *arr, int size, int val){
int linearsearch(int *arr, int size, int val)
{
int i;
for (i = 0; i < size; i++){
for (i = 0; i < size; i++)
{
if (arr[i] == val)
return 1;
}
return 0;
}
void main(){
int n,i,v;
int main()
{
int n, i, v;
printf("Enter the size of the array:\n");
scanf("%d",&n); //Taking input for the size of Array
scanf("%d", &n); //Taking input for the size of Array
int a[n];
printf("Enter the contents for an array of size %d:\n", n);
for (i = 0; i < n; i++) scanf("%d", &a[i]);// accepts the values of array elements until the loop terminates//
for (i = 0; i < n; i++)
scanf("%d", &a[i]); // accepts the values of array elements until the loop terminates//
printf("Enter the value to be searched:\n");
scanf("%d", &v); //Taking input the value to be searched
if (linearsearch(a,n,v))
if (linearsearch(a, n, v))
printf("Value %d is in the array.\n", v);
else
printf("Value %d is not in the array.\n", v);
return 0;
}

View File

@ -2,7 +2,6 @@
#include <stdlib.h>
#define len 5
int binarySearch(int array[], int leng, int searchX)
{
int pos = -1, right, left, i = 0;
@ -10,14 +9,14 @@ int binarySearch(int array[], int leng, int searchX)
left = 0;
right = leng - 1;
while(left <= right)
while (left <= right)
{
pos = left + (right - left) / 2;
if(array[pos] == searchX)
if (array[pos] == searchX)
{
return pos;
}
else if(array[pos] > searchX)
else if (array[pos] > searchX)
{
right = pos - 1;
}
@ -29,10 +28,9 @@ int binarySearch(int array[], int leng, int searchX)
return -1; /* not found */
}
void main(int argc, char *argv[])
int main(int argc, char *argv[])
{
int array[len] = { 5, 8 , 10 , 14 ,16 };
int array[len] = {5, 8, 10, 14, 16};
int position;
position = binarySearch(array, len, 5);
@ -44,5 +42,5 @@ void main(int argc, char *argv[])
printf("The number %d exist in array at position : %d \n", 5, position);
}
return 0;
}

View File

@ -1,24 +1,24 @@
#include <stdio.h>
void stoogesort(int [], int, int);
void main()
void stoogesort(int[], int, int);
int main()
{
int arr[100], i, n;
printf("How many elements do you want to sort: ");
scanf("%d", &n);
for (i = 0;i < n; i++)
for (i = 0; i < n; i++)
scanf(" %d", &arr[i]);
stoogesort(arr, 0, n - 1);
printf("Sorted array : \n");
for (i = 0;i < n;i++)
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
void stoogesort(int arr[], int i, int j)
{
int temp, k;

View File

@ -259,7 +259,7 @@ void insert2(char *s)
Tptr pp, *p;
p = &root;
while (pp = *p)
while (pp == *p)
{
if ((d = *s - pp->splitchar) == 0)
{
@ -390,7 +390,6 @@ void nearsearch(Tptr p, char *s, int d)
nearsearch(p->hikid, s, d);
}
#define NUMBER_OF_STRING 3
int main(int argc, char *argv[])