Showing posts with label Return. Show all posts
Showing posts with label Return. Show all posts

C Program To Returns the nth element of the Fibonacci sequence.

How to write a C Program To Returns the nth element of the Fibonacci sequence in C Programming Language ?


Solution For C Program :

/*C Program To Returns the nth element of the Fibonacci sequence.*/

#include <stdio.h>

/**
 * Returns the nth element of the Fibonacci sequence.
 */
int fibRecursive(unsigned int n)
{
// There is no 0th element of the Fibonacci sequence.
if (n == 0) return -1;

// Base case: 1st or 2nd elements.
if (n == 1 || n == 2) return 1;

// Otherwise, return the sum of the (n-1)th and (n-2)th elements.
return fibRecursive(n - 1) + fibRecursive(n - 2);
}

int main()
{
printf("The fibonacci sequence is:\n");
for (unsigned int n = 1; n < 20; ++n) {
printf("%d\n", fibRecursive(n));
}
printf("...\n");
return 0;
}

C Program To Find Simple Interest

How to write a C Program to find Simple Interest Using function with Parameters and Returning a Value in C Programming Language ?

Solution For C Program :

/*C Program To Find Simple Interest*/

#include <stdio.h>
void main()
{
int p, t;
float r;
float SimpleInterest(int p, int t, float r);
printf("\nEnter the Principle Amount, Term, Rate of Interest : ");
scanf("%d%d%f", &p, &t, &r);
printf("\nThe Simple Interest for the Principle Amount %d for %d Years with an
Interest of %0.2f", p, t, r);
printf("\n is %f.", SimpleInterest(p, t, r));
}
float SimpleInterest(int p, int t, float r)
{
return((p * t * r) / 100);
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

Returns: array of decoded values. [0] - count of values

How to write a C Program to Returns: array of decoded values.  [0] - count of values in C Programming Language ?


Solution:

#include <stdlib.h>
#include <string.h>

/* Returns: array of decoded values.  [0] - count of values */
int *decode(char *encoded) {
    int idx = 1;
    int maxlen = 8;
    char *token;
    char *delim = " :;,|";
    int *nums = malloc(maxlen * sizeof(int));

    /* Preserve the original string */
    encoded = strdup(encoded);

    token = strtok(encoded, delim);
    nums[1] = atoi(token);

    while (token = strtok(NULL, delim)) {
        if (idx >= maxlen) {
            maxlen += 8;
            nums = realloc(nums, maxlen * sizeof(int));
        }
        nums[++idx] = atoi(token);
    }

    nums[0] = idx;
    return nums;
}

C Program Finds Sub-String Search in String

How to write a C Program to Finds Sub-String Search in String in C Programming Language ?

This C Program to Finds sub-string search in string s.
Returns -1 if sub-string was not found
Returns 0 if search was empty
Returns starting index of sub-string if successful

Solution:

  1. int find(string s, string search)
  2. {
  3.         if(length(search) == 0)
  4.         {
  5.                 return 0;
  6.         }
  7.  
  8.         int s_index = 0; //Starting index
  9.         int c_index = 0; //Checking index
  10.         int b_table[length(search)]; //Backtracking table
  11.         fill_backtrack_table(*b_table, search);
  12.  
  13.         while(length(s) >= (s_index + c_index))
  14.         {
  15.                 if(s.src[s_index + c_index] == search.src[c_index])
  16.                 {
  17.                         if(c_index == length(search) - 1)
  18.                         {
  19.                                 return s_index;
  20.                         }
  21.                         else
  22.                         {
  23.                                 c_index++;
  24.                         }
  25.                 }
  26.                 else
  27.                 {
  28.                         if(b_table[c_index] == -1)
  29.                         {
  30.                                 s_index++;
  31.                                 c_index = 0;
  32.                         }
  33.                         else
  34.                         {
  35.                                 s_index += c_index - b_table[c_index];
  36.                                 c_index = b_table[c_index];
  37.                         }
  38.                 }
  39.         }*/
  40.         return -1;
  41. }
  42.  
  43. // Fills the backtrack table for function find
  44. void fill_backtrack_table(int* b_table, string search)
  45. {
  46.         b_table[0] = -1;
  47.         if(length(search) == 1)
  48.         {
  49.                 return;
  50.         }
  51.         b_table[1] = 0;
  52.  
  53.         int t_index = 2; //Table index
  54.         int s_index = 0; //Search index
  55.  
  56.         while (t_index < length(search))
  57.         {
  58.                 if(search.src[t_index - 1] == search.src[s_index])
  59.                 {
  60.                         b_table[t_index] = s_index + 1;
  61.                         s_index++;
  62.                         t_index++;
  63.                 }
  64.                 else if(s_index > 0)
  65.                 {
  66.                         s_index = b_table[s_index];
  67.                 }
  68.                 else
  69.                 {
  70.                         b_table[t_index] = 0;
  71.                         t_index++;
  72.                 }
  73.         }
  74.         return;
  75. }

Return the number of tokens given a command

C Program Return the number of tokens given a command


  1. /* in setup_stack()... */  
  2.  
  3.   int i = 0;
  4.   int argc = get_argc(file_name);
  5.   char *token, *save_ptr;
  6.   char **argv = malloc((argc + 1) * sizeof(char *));
  7.   size_t token_size;
  8.  
  9.   //generate argv and push args onto stack

  10.   for (token = strtok_r((char *)file_name, " ", &save_ptr); token != NULL; token = strtok_r(NULL, " ", &save_ptr)) {
  11.     token_size = strlen(token) + 1;
  12.     *esp = *esp - token_size;
  13.     argv[i] = *esp;
  14.     i++;
  15.     memcpy(*esp, token, token_size);
  16.   } argv[argc] = 0;
  17.  
  18.   //perform alignment

  19.   size_t alignment = (size_t) *esp % 4;
  20.   if (alignment) {
  21.     *esp = *esp - alignment;
  22.     memcpy(*esp, &argv[argc], alignment);
  23.   }
  24.  
  25.   //push argv pointers

  26.   for (= argc; i >= 0; i--) {
  27.     *esp = *esp - sizeof(char *);
  28.     memcpy(*esp, &argv[i], sizeof(char *));
  29.   }
  30.  
  31.   //push pointer to argv

  32.   char *ptr = *esp;
  33.   *esp = *esp - sizeof(char **);
  34.   memcpy(*esp, &ptr, sizeof(char **));
  35.  
  36.   //push argc
  37.   *esp = *esp - sizeof(int);
  38.   memcpy(*esp, &argc, sizeof(int));
  39.  
  40.  //push dummy return address

  41.   *esp = *esp - sizeof(void *);
  42.   memcpy(*esp, &argv[argc], sizeof(void *));
  43.  
  44.   free(argv);
  45.   return success;
  46. }
  47.  
  48. /* Return the number of tokens given a command 'file_name.' */

  49. int get_argc(const char* file_name) {
  50.     int argc = 0;
  51.     char *buffer, *token, *save_ptr;
  52.     buffer = malloc(sizeof(file_name));
  53.     strlcpy(buffer, file_name, sizeof(file_name));
  54.     for (token = strtok_r((char *)buffer, " ", &save_ptr); token != NULL; token = strtok_r(NULL, " ", &save_ptr)) {
  55.         argc++;
  56.     }
  57.     free(buffer);
  58.     return argc;
  59. }