Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute C99 Online

c99

#include<stdio.h>
#include<stdlib.h>
int main()
{
int i;
FILE *fp;
 fp= fopen("foo.dat","w");
if(NULL== fp){
fprintf(stderr,"foo.dat は fopen できません \n");
exit(1);
}
for(i=100;i<110;i++){
fprintf(fp,"%d\n",i);
}
fclose(fp);
 return 0;
}

Compile and Execute C99 Online

c99

#include <stdio.h>
#include <string.h>

char first[] = "Pimp juice";
char last[] = "Harding";
char fullname[50] = "default";

int main()
{
    
   // strcat number(4) = "First Name, Last Name";
    printf("This is the Last Name: %s\n", last);
    printf("This is the first name: %s\n", first);
    printf("This is FullName: %s\n", fullname);
    
    //strcpy(destination, source)
    strcpy(fullname, first);
    printf("This is full Name: %s\n",fullname);
    
    strcat(fullname," ");
    
    strcat(fullname, last);
    printf("Hello: %s\n",fullname);
    printf("First name is %d characters long.\n",strlen(first));
    printf("Last name is %d \t\t\tcharacters long.\n"  ,   strlen(last));
    printf("Full name is %d characters long.\n",strlen(fullname ));
    printf("Your Initials are %c. %c.\n",first[2],last[0]);
    printf("The ASCII decimal initials are %d. %d.\n",first[0],last[0]);
return 0;
}

Implement a stack with push(), pop() and GetMin() in O(1) time, with GetMin() using O(1) space

c99

//Project Name : Implement a stack with push(), pop() and min() in O(1) time,
//--getMin O(1) time using O(1) space
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>

typedef struct node {
	int val;
	struct node *next;
}NODE;

int stackMin;

NODE * new_node(int v)
{
	struct node *tmp;

	tmp = (struct node *)malloc(sizeof(struct node));
	tmp->val = v;
	return tmp;
}



void stack_push(NODE **head_ref,  int val)
{
    NODE *pt, *tmp;
    pt = (NODE *)malloc(sizeof(NODE));
    
    if((*head_ref == NULL))
    {
        pt->val = val;
        pt->next = NULL;
        stackMin = val;
        
        *head_ref = pt;
        //return;
        goto exit;
    }
    //push to the regular stack
    if(val > stackMin)
    {
        pt->val = val;
        
    }else
    {
        pt->val = val - stackMin; //new val is the diff of new Min and old Min
        stackMin = val; //the new Min
    }

    pt->next = *head_ref; //current head
    *head_ref = pt; //new head
exit:
    //printf("push_item:%d stack_top: %d, stackMin: %d\n", val, pt->val, stackMin);
    return;
    
}

int stack_pop(NODE **head_ref)
{
    NODE *pt;
    pt = *head_ref;
    
    int val, tmp;
    val = pt->val;
    
    tmp = stackMin;
    
    if (pt != NULL)
    {
        *head_ref = pt->next;
        free(pt);
    }
    if (val < stackMin)
    {
        stackMin = stackMin - val; //val is the diff of new Min and old Min
    //---^^L older Min ^^: newer Min ---   
        return (tmp);
        //return stackMin;
    }else
        return val;
}

int stack_getMin(NODE **head_ref)
{
    return (stackMin);    
}
void printList(NODE *p)
{
    NODE *pt = p;
    //printf("\n");
    while(pt != NULL )
    {
        printf("[%d] ", pt->val);
        pt = pt->next;
    }
    printf("\n");
}
int main(void)
{	
	NODE *n1, *n2, *n3, *n4, *n5, *n6,*p;
	NODE *head=NULL; //regular stack
	NODE *head_s=NULL; //special stack
#if 0	
	n1 = new_node(1); n1->next = NULL;
	n2 = new_node(2); n2->next = n1;
	n3 = new_node(3); n3->next = n2;
	n4 = new_node(4); n4->next = n3;
	n5 = new_node(5); n5->next = n4;
	n6 = new_node(6); n6->next = n5;
    printList(n6);

    printf("\nreversed linklist: \n");
    p = reverse_linklist(n6, 2);
    printList(p);	
#else
    printf("stack_push: 18, 19, 29, 15, 16\n");
    stack_push(&head, 18); 
    stack_push(&head, 19);
    stack_push(&head, 29);
    stack_push(&head, 15);
    stack_push(&head, 16);
    //stack_push(&head,&head_s, 6);
    printList(head);
    //printList(head_s);
    while(head !=NULL)
    {
       // printf("POP: %d\n", stack_pop(&head,&head_s));
        printList(head);
        //printList(head_s);
        printf("getMin(): %d\n", stack_getMin(&head));
        printf("POP: %d\n", stack_pop(&head));
        printf("\n");
    }

    //printf("\nreversed linklist: \n");
    //p = reverse_linklist(head, 2);
    //printList(p);    
#endif

}

Egg Dropping Puzzle (dynamic programming) in C99

c99

#include <stdio.h>
#include <limits.h>

/*
The following is a description of the instance of this famous puzzle involving n=2 eggs and a building with k=36 floors.

Suppose that we wish to know which stories in a 36-story building are safe to drop eggs from, and which will cause the eggs to break on landing. We make a few assumptions:

…..An egg that survives a fall can be used again.
…..A broken egg must be discarded.
…..The effect of a fall is the same for all eggs.
…..If an egg breaks when dropped, then it would break if dropped from a higher floor.
…..If an egg survives a fall then it would survive a shorter fall.
…..It is not ruled out that the first-floor windows break eggs, nor is it ruled out that the 36th-floor do not cause an egg to break.

If only one egg is available and we wish to be sure of obtaining the right result, the experiment can be carried out in only one way. Drop the egg from the first-floor window; if it survives, drop it from the second floor window. Continue upward until it breaks. In the worst case, this method may require 36 droppings. Suppose 2 eggs are available. What is the least number of egg-droppings that is guaranteed to work in all cases?
The problem is not actually to find the critical floor, but merely to decide floors from which eggs should be dropped so that total number of trials are minimized.
*/


//#define max(x, y) (x>y)? x: y 
//macro doesn't work here--yd

int max(int a, int b) { return (a > b)? a: b; }


int egg_drop(int eggs,int floors)
{
    // Memoize results in a 2D array, where memo[i][j] = egg_drop(i, j);
    int memo[floors + 1][eggs + 1];
    
    // Traverse through all floors
    for (int floor = 0; floor <= floors; floor++)
    {
        // Traverse through all eggs
        for (int egg = 0; egg <= eggs; egg++)
        {
            // Case where no drops are necessary
            if (floor == 0 || egg == 0)
            {
                memo[floor][egg] = 0;
            }
            
            // Case where only one drop is necessary since there is only one floor
            else if (floor == 1)
            {
                memo[floor][egg] = 1;
            }
          
          // Case where the numbers of drops equals the number of floors, since 
          // there is only one egg available
            else if (egg == 1)
            {
                memo[floor][egg] = floor;
            }
            
            else
            {
                // Keep track of the MINIMUM number of drops needed
                memo[floor][egg] = INT_MAX;
            
                // Consider each drop from first to current floor
                for (int x = 1; x <= floor; x++)
                {
                    // If the egg breaks on floor x, we only need to test floors up to the
                    // x-1th floor. We also have one less egg since the egg broke.
                    int egg_break = memo[x - 1][egg - 1];
                    
                    // If the egg did not break on floor x, we only need to test floors 
                    // starting from the floor-xth floor onwards. The number of eggs 
                    // does not change since the egg did not break.
                    int egg_not_break = memo[floor - x][egg];
                    
                    // Find the number of drops necessary in the WORST CASE scenario.
                    // Don't forget to add one to include the current drop.
                    int drops = max(egg_break, egg_not_break) + 1;
                    
                    // Update memo[floor][egg] if we found a smaller number of needed 
                    // egg drops
                    if (drops < memo[floor][egg])
                    {
                        memo[floor][egg] = drops;
                    }
                }
            }
        }
    }
    return memo[floors][eggs];
}

int main()
{
    //int n = 2, k = 10; //anwser: 4
    int n = 2, k = 36; //anwser: 8
    
    printf ("\nMin no. of trials in worst case with %d eggs and "
             "%d floors is %d \n", n, k, egg_drop(n, k));

    return 0;
}

Merge soft in C99

c99

#include <stdio.h>

void merge(int arr[], int l, int m, int r)
//arr: two sorted subarray to be merged
//arr[l]~arr[m]: left sorted subarray, arr[m]~arr[r]: right sorted subarray
{
    int n1 = m-l+1; //items in first half
    int n2 = r-(m+1)+1; //items in second half

    int L[n1]; //first temp array
    int R[n2]; //second temp array
    
    int i=0; //index of first temp array
    int j=0; //index of second temp array
    int k=l; //inde of merged array, starting at l
    
    //copy to temp array
    for (int ii=0; ii<n1; ii++)
        L[ii] = arr[l+ii];
    for(int ii=0; ii<n2; ii++)
        R[ii] = arr[m+1+ii];
        
    //merge
    while (i<n1 && j <n2)
    {
        if(L[i] < R[j])
        {
            arr[k] = L[i];//smaller one: pick from L
            i++; //advance in L
        }else
        {
            arr[k] = R[j];//smaller one: pick from R
            j++;//advance in R
        }
        k++; //advance the index in the merged array
    }
    
    //copy whatever left in L (pre-sorted)
    while(i < n1)
    {
        arr[k] = L[i];
        k++; i++;
    }
    //copy whatever left in R (pre-sorted)
    while(j < n2)
    {
        arr[k] = R[j];
        k++; j++;
    }
    
}


void merge_sort(int arr[], int l, int r)
{
    if (l < r)
    {
        int m = l+ (r-l)/2; //same as (r+l)/2 but avoid the overflow for large l/h
        merge_sort(arr, l, m);
        merge_sort(arr, m+1, r);
        merge(arr, l, m, r);
    }
}
void printArray(int A[], int n)
{
    for (int i=0; i<n; i++)
    {
        printf("%d ", A[i]);
    }
    printf("\n");
}
int main()
{
    int arr[] = {12, 11, 13, 5, 6, 7};
    int arr_size = sizeof(arr)/sizeof(int);
    
    printArray(arr, arr_size);
    merge_sort(arr, 0, arr_size-1);
    printArray(arr, arr_size);
    
    return 0;
}

Compile and Execute C99 Online

c99

#include <stdio.h>
#include <stdlib.h>

typedef struct _Node{
    int val;
    struct _Node *left, *right;
} Node;

Node *newNode(int val)
{
    Node *p = (Node *)malloc(sizeof(Node));
    p->val = val;
    p->left = p->right = NULL;
}

int countNodes(Node *root)
{
    int n = 0;
    if(root == NULL) return 0;
    else
        return countNodes(root->left) + countNodes(root->right) + 1;
    
}

void storeInorder(Node *node, int inorder[], int *index_ptr )
{
    if(node == NULL) return;
    
    storeInorder(node->left, inorder, index_ptr);
    
    inorder[*index_ptr] = node->val;
    (*index_ptr) ++;
    
    storeInorder(node->right, inorder, index_ptr);
}
void printInorder(Node *root)
{
    if(!root) 
        return;
    printInorder(root->left);
    printf("%d ", root->val);
    printInorder(root->right);
}
void arrayToBST (int *arr, Node *root, int *index_ptr)
{
    if(root == NULL) return;
    
    arrayToBST(arr, root->left, index_ptr);
    root->val = arr[*index_ptr];
    (*index_ptr)++;
    arrayToBST(arr, root->right, index_ptr);
}
int compare(const void *a, const void *b)
{
    return (*(int *)a - *(int *)b);
}

void binaryTreeToBST (Node *root)
{
    int no = countNodes(root); 
    int inorder[no];
	int index_ptr;
	index_ptr = 0;
	storeInorder(root, inorder, &index_ptr );

	qsort(inorder, no, sizeof(int), compare);

	index_ptr = 0;
	arrayToBST(inorder, root, &index_ptr);
	
}
/* Driver function to test above functions */
int main()
{
	Node *root = NULL;

	/* Constructing tree given in the above figure
    		  10
    		 / \
    		30 15
        	/	 \
           20	 5
           
    to a BST as following:
              15
             /  \
            10  20
            /    \
           5     30
    */
	root = newNode(10);
	root->left = newNode(30);
	root->right = newNode(15);
	root->left->left = newNode(20);
	root->right->right = newNode(5);
	
	
    printf("Inorder Traversal of the original tree :\n");
    printInorder(root); 
    printf("\n");
	// convert Binary Tree to BST
	binaryTreeToBST (root);

	printf("Following is Inorder Traversal of the converted BST: \n");
	printInorder (root);

	return 0;
}

nein

c99

#include <stdio.h>

int main()
{
    int a;
    while(a = 1) {
        puts("fresse\n");
    }
    return 0;
}

//https://pt.stackoverflow.com/q/286742/101

c99

#include <stdio.h>
#include <stdlib.h>

void BinFormat(char character, char *text) {
	text[8] = '\0';
    for (int i = 7; i >= 0; i--) text[i] = ((character >> (i - 1)) & 1) + '0';
}

int main(void) {
	char *text = malloc(9);
	BinFormat('A', text);
	printf("%s", text);
}

//https://pt.stackoverflow.com/q/286742/101

sizeof function implementation

c99

#include <stdio.h>

#define my_sizeof(type) (char *)(&type + 1) - (char *)(&type)

int main()
{
    int a[5];
    
    unsigned long int b;
    
    printf("size of array is %d\n", sizeof(a));
    
    printf("my_sizeof is %d\n", my_sizeof(a));
    
    printf("size of datatype is %d\n", sizeof(double));
    
    printf("size of datatype is %d\n", my_sizeof(double));
    
    return 0;
}

Compile and Execute C99 Online

c99

#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>

int printdir(char *d){
    DIR *dir;
    
    struct stat finfo;
    struct dirent *files;
    char curfile[256];
    
    dir = opendir(d);
    if(!dir)return 0;
    while(files = readdir(dir)){
        if(files->d_name[0]!='.'){
            sprintf(curfile,"%s/%s",d,files->d_name);
            
            stat(curfile,&finfo);
            if(S_ISDIR(finfo.st_mode)){
               puts(curfile);
             //int k = printdir(curfile);           
             }else{
                printf("%s",curfile);    
            }
            
        }
    }
}


int alldir(char *d){
    struct stat finfo;
    if(stat(d,&finfo)<0)return 0;
    
    
}



int main(){
    printdir("/home/teste");
    
}

Previous 1 ... 3 4 5 6 7 8 9 ... 13 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.