Sorrendben látható tornyok magasságának összeírása új tömbben

Sorrendben látható tornyok magasságának összeírása új tömbben

  1. #include <stdio.h>
  2. // Sorrendben látható tornyok magasságának összeírása új tömbben
  3. int main() {
  4.         int tomb[8]={1,50,32,30,41,61,72,40};
  5.         int i,j,z=0,l=1,tomb2[8],hossz=0,k=0,tomb3[8];
  6.         tomb2[0]=tomb[0];      
  7.         for(i=0;i<8;i++){
  8.                 for(j=0;j<i;j++){              
  9.                         if(tomb[i]>tomb[j]){
  10.                                 tomb2[l]=tomb[i];
  11.                         }else{
  12.                                 tomb2[l]=0;
  13.                                 break;
  14.                         }
  15.                 }
  16.         l++;           
  17.         }
  18.         //logikai megvalósítás eddig tart
  19.         //rendezés és átírás új tömbbe
  20.         for(i=0;i<l;i++){
  21.                 if(tomb2[i]!=0){
  22.                 tomb3[k]=tomb2[i];
  23.                 hossz++;
  24.                 k++;
  25.                 }
  26.        
  27.         }
  28.         hossz--;
  29.         k--;
  30.         int rendezes[hossz];
  31.         for(i=0;i<hossz;i++){
  32.                 rendezes[i]=tomb3[i];
  33.         }
  34.         for(i=0;i<hossz;i++){
  35.                 printf("%d\t",tomb3[i]);
  36.         }
  37.         printf("\n");
  38.         return 0;
  39. }

Factorial in C Programming Language

How to write a C Program Factorial in C Programming Language Example ?

Solution:
  1. #include <stdio.h>
  2.  
  3.  
  4. int fac1(int n, int acc){
  5.    return (> 0 ) ? fac1(n-1, n*acc) : acc ;
  6. }
  7.  
  8. int fac(int n){
  9.    int acc = 1;
  10.    return fac1(n, acc);
  11. }
  12.  
  13. int fac2(int n){
  14.    int acc = 1;
  15.    for(int i = 0; i < n; i++)
  16.       acc *= (n-i);
  17.    return acc;
  18. }
  19. int main(int argc, char *argv[]) {      
  20.    int num;
  21.    scanf("%d", &num);
  22.    printf("%d\n", fac(num));    
  23.    printf("%d\n", fac2(num));  
  24.         return 0;
  25. }

= (int*)malloc(sizeof(int)) ??

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. void read(FILE* input, int *n, float *atlseb){
  6.     fscanf(input, "%d", *n);
  7. }
  8.  
  9. int main(){
  10.  
  11.     FILE* input = fopen("input.txt", "r");
  12.     FILE* output = fopen("output.txt", "w");
  13.  
  14.     if(input == NULL){
  15.         fprintf(output, "Hibas Input!\n");
  16.     }
  17.  
  18.     int */*= (int*)malloc(sizeof(int)) ??*/;
  19.     float *atlseb;
  20.  
  21.     read(input, *n, *atlseb);
  22.     fprintf(output, "%d", &n);
  23.  
  24. return 0;
  25. }

Design a C function that shortens a string to 8 characters

How to design a C function that shortens a string to 8 characters in C Programming Language ?


Solution:

  1. #include "base.h"
  2. #include "string.h"
  3. /*
  4. Design a function that shortens a string to 8 characters.

  5. */
  6. // String -> String    
  7. String truncate(String string);
  8. void truncate_test() {
  9.     check_expect_s(truncate("1234567891011"), "12345678");      // line 19
  10.     check_expect_s(truncate("123"), "123");                             // line 20
  11.     check_expect_s(truncate("12345678"), "12345678");           // line 21
  12. }
  13. // Returns the original string or shortens it to the first 8 characters.
  14. String truncate(String string) {
  15.     if (s_length(string) <= 8) {
  16.         return(string);
  17.     } else {
  18.         return(s_sub(string, 0,8));
  19.     }
  20.         return 0;
  21. }
  22. int main(void) {
  23.     truncate_test();
  24.     return 0;
  25.     }

C Program to Implements a dictionary's functionality

How to write a C Program to Implements a dictionary's functionality in C Programming Language ?

Solution:

  1. /****************************************************************************
  2.  * dictionary.c
  3.  * Implements a dictionary's functionality.

  4.  ***************************************************************************/
  5. #include <stdbool.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include "dictionary.h"
  9. // declare structure for each word
  10.     typedef struct node
  11.     {
  12.         char word[LENGTH + 1];
  13.         struct node* next;
  14.     }
  15.     node;
  16.    
  17. bool hash(const char* word)
  18. {
  19.     // index < the number of buckets
  20.     // deterministic - the same value needs to map to the same bucket every time
  21. }
  22. /**
  23.  * Returns true if word is in dictionary else false.
  24.  */
  25. bool check(const char* word)
  26. {
  27.     // TODO
  28.     return false;
  29. }
  30. /**
  31.  * Loads dictionary into memory.  Returns true if successful else false.
  32.  */
  33. bool load(const char* dictionary)
  34. {
  35.    
  36.     // TODO
  37.    
  38.    
  39.     // declare file pointer for dictionary file
  40.     FILE* input = NULL;
  41.     // open dictionary
  42.     input = fopen(dictionary, "r");
  43.     if (input == NULL)
  44.     {
  45.         return false;
  46.     }
  47.     // declare memeory space or each word
  48.     node* new_node = malloc(sizeof(node));
  49.     while(input != EOF)
  50.     {
  51.     // scan dictionary after opening for each word before a\n and stores in in new_code->word
  52.     fscanf(input, "%s", new_node->word);
  53.     // hash the word (takes string and returns an index)
  54.     hash(word);
  55.     // this is a example on how to put a word in the front of the list
  56.     new_node->next = head;
  57.     head = new_node;
  58.     }
  59.    
  60.     return false;
  61. }
  62. /**
  63.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  64.  */
  65. unsigned int size(void)
  66. {
  67.     // TODO
  68.     return 0;
  69. }
  70. /**
  71.  * Unloads dictionary from memory.  Returns true if successful else false.
  72.  */
  73. bool unload(void)
  74. {
  75.     // TODO
  76.     return false;
  77. }