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 C Online

c

#include <stdio.h>

int main()
{
    //a comments of one line
    /*
        a bloc of comments of multiple line
        here A, B, and C are integer variables
        
        C is case sensitive: int a is differ from int A
        C is space insensitive
    */
float z;
    int A, B, C //still valid not nice syntax
    ;
    A=5;
    B    =   10;
    C=50;z=4.5;
    printf("value A is %d and the value of B is %d /n and value C is %d ", A, B, C );
    printf("hi!\t");
    printf("Hello, World!\n");

    return 0;
}

while文(課題)

c

#include <stdio.h>

int main(void)
{
    int sum = 0;
    while(sum <= 100){
        if(sum%3 == 0 && sum%5 == 0){
            printf("CUSPIDE\n");
        }else if(sum%3 == 0){
            printf("CUS\n");
        }else if(sum%5 == 0){
            printf("PIDE\n");
        }else{
            sum = sum + 1;
            printf("%d\n",sum);
        }
    }
    return 0;
}

Perfect number

c

#include <stdio.h>

int main()
{
    int i, num, sum = 0 ; 
    int size ,j, a[10],n1;
    printf("\nEnter the size of array:");
    scanf("%d",&size); 
    printf("\n enter array elements \n");
    scanf("%d",&a[j]);
    for (j=1;j<n1;j++)
    {
        if(n1 % j == 0)
        {
            sum += j ; 
        }
    }
    if (sum == n1)
    return 1 ;
    else
    return 0 ; 
    /* Input a number from user */
    printf("Enter any number to check perfect number: ");
    scanf("%d", &num);

    /* Calculate sum of all proper divisors */
    for(i=1; i<num; i++)
    {
        /* If i is a divisor of num */
        if(num%i == 0)
        {
            sum += i;
        }
    }

    /* Check whether the sum of proper divisors is equal to num */
    if(sum == num)
    {
        printf("%d is PERFECT NUMBER", num);
    }
    else
    {
        printf("%d is NOT PERFECT NUMBER", num);
    }

    return 0;
}

Daniel - how to update values by reference

c

#include <stdio.h>

void update_value(int *value)
{
    // WRITE CODE to change value to 1234
    //
    
}

int main()
{
    printf("Hello, World!\n");
    int value = 88888888; // set value for debug
    printf("Before value = %d\n", value);    
    update_value(&value);
    printf("After  value = %d\n", value);        
    return 0;
}

Daniel - double-linked list

c

/////////////////////////////////////////////////////////////////////////////
// INTEGRITY INSTRUCTIONS

// Explicitly state the level of collaboration on this question
// Examples:
//   * I discussed ideas with classmate(s) [include name(s)]
//   * I worked together with classmate(s) in the lab [include name(s)]
//   * Classmate [include name] helped me debug my code
//   * I consulted website [include url]
//   * None
// A "None" indicates you completed this question entirely by yourself
// (or with assistance from course staff)
/////////////////////////////////////////////////////////////////////////////
// INTEGRITY STATEMENT:
// I received help from the following sources:

// None

// Name: Daniel Weintraub
// login ID: dweintra
/////////////////////////////////////////////////////////////////////////////

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

/*
C: creates a new list; must be the first call in your test
D: destroys the existing list
Q: quits the program
IF n: inserts an node with value n at the front of the list (list_insert_front(..))
IB n: inserts an node with value n at the back of the list (list_insert_back(..))
RF: removes the front node and returns its value (list_remove_front(..))
RB: removes the back node and returns its value (list_remove_back(..))
ROF n: attempts to remove the first node with value n from the list (list_remove_occ_first(..))
ROL n: attempts to remove the last node with value n from the list (list_remove_occ_last(..))
PF: prints an existing list (list_print(false))
PR: prints an existing list in reversed order (list_print(true))
*/

// THERE IS NO NEED TO CHANGE THIS STRUCT
struct llist {
  struct node *first;
  struct node *last;
  int length;
};

// THERE IS NO NEED TO CHANGE THIS STRUCT
struct node {
  struct node *next;
  struct node *prev;
  int value;
};

struct llist *list_create(void) {
  struct llist *list = malloc(sizeof(struct llist));
  list->first = NULL;
  list->last = NULL;
  list->length = 0;
  return list;
}

void list_destroy(struct llist *list) {
  assert(list);
  struct node *current = list->first;
  while (current != NULL) { // travers nodes
    struct node *to_free = current;
    current = current->next;
    free(to_free);
  }
  free(list);
}

struct node* create_new_node(const int value)
{
  struct node* new_node = malloc(sizeof(struct node)); 
  new_node->value = value; 
  new_node->next = NULL; 
  new_node->prev = NULL;
  return new_node;        
}

void list_insert_front(struct llist *list, const int value) {
  assert(list);
  struct node* new_node = create_new_node(value);

  if (list->first == NULL) { // list is empty
    list->first = new_node;
    list->last = new_node;
  } else {
    new_node->next = list->first;
    list->first = new_node;
    list->first->next->prev = list->first;
  }
  
  list->length++;
}

void list_insert_back(struct llist *list, const int value) {
  assert(list);
  struct node* new_node = create_new_node(value);  
  
  if (list->length == 0) { // list is empty
    list->first = new_node;
    list->last = new_node;
  } else {
    //struct node *current = list->first;
    //while (current->next != NULL) { // find last node
    //  current = current->next;
    //}
    new_node->prev = list->last;
    list->last->next = new_node;
    list->last = new_node;
  }
  list->length++;
}

bool list_remove_front(struct llist *list, int *value) 
{
  bool result = true;
  
  if(list->length == 1){
    *value = list->first->value;      
    free(list->first);
    list->last = NULL;
    list->first = NULL;
    list->length = 0;
  } 
  else if(list->length > 1)
  {
    struct node *tmp_first = list->first;      
    *value = list->first->value;            
    list->first = list->first->next;
    list->first->prev = NULL;
    list->length--;
    free(tmp_first);
  } 
  else 
  {
    result = false;
  }
  return result;
}

bool list_remove_back(struct llist *list, int *value) {
  bool result = true;
  if(list->length == 1)
  {
    *value = list->last->value;      
    free(list->last);    
    list->last = NULL;
    list->first = NULL;
    list->length = 0;
  } 
  else if(list->length  > 1)
  {
    struct node *tmp_last = list->last;
    *value = list->last->value;      
    list->last = list->last->prev;
    list->last->next = NULL;
    list->length--;
    free(tmp_last);
  } 
  else 
  {
    result = false;
  }
  return result;
}

void remove_and_free_node(struct llist *list, struct node *tmp_node)
{
    assert(list);
    assert(tmp_node);
  if(tmp_node->next == NULL && tmp_node->prev == NULL)
  {
    // the only node  
    list->first = NULL;
    list->last = NULL;
  }
  else if(tmp_node->next != NULL && tmp_node->prev != NULL)
  {
    // not the first, not the last  
    tmp_node->prev->next = tmp_node->next;
    tmp_node->next->prev = tmp_node->prev;
  } 
  else if(tmp_node->prev == NULL)
  {
    // first node              
    list->first = tmp_node->next;
    list->first->prev = NULL;
  }
  else
  {
    // last node
    list->last = tmp_node->prev;
    list->last->next = NULL;
  }
  
  free(tmp_node);
  list->length--;
}


bool list_remove_occ_first(struct llist *list, int value) 
{
  bool found = false;
  assert(list);
  struct node *tmp_node = list->first;
  while(tmp_node)
  {
      if(tmp_node->value == value)
      {
          remove_and_free_node(list, tmp_node);
          found = true;
          break;
      }
      
      tmp_node = tmp_node->next;
  }
  return found;
}

bool list_remove_occ_last(struct llist *list, int value) 
{
  bool found = false;
  assert(list);
  struct node *tmp_node = list->last;
  while(tmp_node)
  {
      if(tmp_node->value == value)
      {
          remove_and_free_node(list, tmp_node);
          found = true;
          break;
      }
      
      tmp_node = tmp_node->prev;
  }
  return found;  
}

// DO NOT MODIFY list_print(..)!
void list_print(const struct llist *list, bool reverse) {
  assert(list);
  struct node *current = (reverse) ? list->last : list->first;
  if (current == NULL) {
    printf("List empty\n");
  } else {
    int i = (reverse) ? list->length - 1 : 0;
    while(current != NULL) {
      printf("Elem #%d/%d: %d\n", i, list->length - 1, current->value);
      if (reverse) {
        current = current->prev;
        i--;
      } else {
        current = current->next;
        i++;
      }
    }
  }
}


int main(void) {
  struct llist *dll;
  char cmd;
  int val;

  while(true) 
  {
    scanf(" %c", &cmd);
    if (cmd == 'C') 
    {            // list_create
      printf("Creating list\n");
      dll = list_create();
    } 
    else if (cmd == 'D') 
    {     // list_destroy
      printf("Deleting list\n");    
      list_destroy(dll);
    } 
    else if (cmd == 'I') 
    {     // list_insert ...
      scanf(" %c", &cmd);
      scanf(" %d", &val);
      printf("Inserting node %c %d\n", cmd, val);                
      if (cmd == 'F') {          // ... _front
        list_insert_front(dll, val);
      } else if (cmd == 'B') {   // ... _back
        list_insert_back(dll, val);
      }
    } 
    else if (cmd == 'R') 
    {     // list_remove ...
      scanf(" %c", &cmd);
      printf("Remove command %c\n", cmd);                                
      if (cmd == 'F') {          // ... _front
        val = 8888; //debug data for test
        if (list_remove_front(dll, &val))
        {
            printf("Removed node with value %d\n", val);                              
        }
      } 
      else if (cmd == 'B') 
      {   // ... _back
        val = 8888; //debug data for test      
        if (list_remove_back(dll, &val))
        {
            printf("Removed node with value %d\n", val);                                      
        }
      } 
      else if (cmd == 'O') {   // ... _occurance ...
        scanf(" %c", &cmd);
        scanf("%d", &val);
        printf("Remove occurance of %d from %c\n", val, cmd);                              
        if (cmd == 'F') 
        {        // ... _first
          if (list_remove_occ_first(dll, val))
          {
            printf("Removed first occurence of node with value %d\n", val);                                                   
          }
        } 
        else if (cmd == 'L') 
        { // ... _last
            if(list_remove_occ_last(dll, val))
            {
                printf("Removed last occurence of node with value %d\n", val);                         
            }
        }
      }
    } else if (cmd == 'P') {     // print_list:
      scanf(" %c", &cmd);
      if (cmd == 'F') {          // (forward)
        list_print(dll, false);
      } else if (cmd == 'R') {   // (reversed)
        list_print(dll, true);
      }
    } else if (cmd == 'Q') {     // quit program
      printf("Exiting\n");    
      break;
    }
  }
  return EXIT_SUCCESS;
}

pointers intro

c

#include <stdio.h>

int main()
{
    int i;
    double d;
    float f;
    char ch;
    i = 10;
    d = 143.12;
    f = 123.4;
    ch = 'a';
    
    int *ip = &i; // pointer stores the address of another variable
    double *dp = &d;
    float *fp = &f;
    char *cp = &ch;
    
    // ip,dp,fp,cp indicates the address of the different values stored.
    // *ip,*dp,*cp,*fp indicates the values stored.
    
    printf("the value of i is %d\n",i);
    printf("the value of d is %f\n",d);
    printf("the value of f is %f\n",f);
    printf("the value of ch is %c\n",ch);
    
    printf("\n");
    
    printf("the address of i is %x\n",ip);
    printf("the address of d is %x\n",dp);
    printf("the address of f is %x\n",fp);
    printf("the addres of ch is %x\n",cp);
    
    printf("\n");
    
    printf("the size of i:%d\n",sizeof(i));
    printf("the size of f:%d\n",sizeof(f));
    printf("the size of d:%d\n",sizeof(d));
    printf("the size of ch:%d\n",sizeof(ch));
    
    printf("\n");
    
    printf("the size of ip:%d\n", sizeof(ip));
    printf("the size of dp:%d\n",sizeof(dp));
    printf("the size of fp:%d\n",sizeof(fp));
    printf("the size of cp:%d\n",sizeof(cp));
    
    printf("\n");
    
    printf("the value of i at ip is: %d %x\n", *ip, ip);
    printf("the value of f at fp is:%f %x\n", *fp, fp);
    printf("the value of ch at cp is:%c %x\n", *cp, cp);
    printf("the value of d at dp is: %f %x\n", *dp, dp);
    
    return 0;
}

Cyklus do while - Kalkulačka

c

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

int main(int argc, char** argv) 
{

double a;
double b;
int volba; // Mohli bychom použít char, abychom zbytečně nepoužívali moc místa
double vysledek;
char pokracovat; //neni potreba nastavovat
printf("Vitejte v kalkulacce \n");
do
{
    printf("Zadejte prvni cislo: \n");
    scanf("%lf",&a);
    printf("Zadejte druhe cislo: \n");
    scanf("%lf",&b);
    printf("Zvolte si operaci: \n");
    printf("1 - scitani \n");
    printf("2 - odcitani \n");
    printf("3 - nasobeni \n");
    printf("4 - deleni \n");
    scanf("%d", &volba);
    switch(volba)
    {
        case 1:
            vysledek = a + b;
            break;
        case 2:
            vysledek = a - b;
            break;
        case 3:
            vysledek = a * b;
            break;
        case 4:
            vysledek = a / b;
            break;
    }
    if ((volba > 0) && (volba < 5))
        printf("Vysledek: %lf\n", vysledek);
    else
        printf("Neplatna volba \n");
    printf("Prejete si zadat dalsi priklad? [1/0]");
    scanf("%d",  &pokracovat);
} 
while (pokracovat == 1);
printf("Dekuji za pouziti kalkulacky");
return;
}

Cyklus for - Mocninátor

c

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

int main(int argc, char** argv) {

int i;
int a;
int n;
int vysledek;
printf("Mocninator \n");
printf("========== \n");
printf("Zadejte zaklad mocniny: \n");
scanf("%d", &a);
printf("Zadejte exponent: \n");
scanf("%d", &n);

vysledek = a;
for (i = 0; i < (n - 1); i++)
    vysledek = vysledek * a;

printf("Vysledek: %d\n", vysledek);
printf("Dekuji za pouziti mocninatoru \n");
	return (EXIT_SUCCESS);
}

LeetCodeTest

c

#include <stdio.h>

int maxArea(int* height, int heightSize) {
    int i,j=0;
    int maxarea,temparea,temphigh,tempwidth=0;
    for(i=0;i<heightSize-1;i++)
    {
        for(j=i+1;j<heightSize;j++){
            if(*(heighht+i))>(*(heighht+j)){
                temphigh=*(heighht+j);
            } 
            else{
                temphigh=*(heighht+i);
            }
            
            temparea=temphigh*tempwidth;
            if (temparea>maxarea)
            {
                maxarea=temparea;
            }    
        }
    }
    return temparea;
    
}
int main()
{
 
    int ans;
    heigh[]=(1,8,6,2,5,4,8,3,7);
    *heightinput=heigh;
    ans=maxArea(heightinput,9)
    printf("ans:%d",ans);
    printf("Hello, World!\n");
    return 0;
}

Advertisements
Loading...

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