Showing posts with label Define. Show all posts
Showing posts with label Define. Show all posts

C program to defines a generic type for queues

How to write a C program to defines a generic type for queues in C Programming Language ?

This C program defines a generic type for queues.

Solution:

  1. /* This C program defines a generic type for queues. */
  2. #ifndef QUEUE_H
  3. #define QUEUE_H
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdbool.h>
  8.  
  9. typedef struct Queue Queue;
  10. typedef int Queueelem;
  11.  
  12. Queue *queue_new(unsigned long queue_size);
  13. bool queue_is_empty(const Queue *q);
  14. void queue_add(Queue *q, Queueelem elem);
  15. Queueelem queue_get(Queue *q);
  16. void queue_show(const Queue *q);
  17. void queue_delete(Queue *q);
  18.  
  19. #endif

C Program to Defined Node with Example

How to write a C Program to defined Node in C Programming Language ?

Solution:


/* Node is defined as :
typedef struct node
{
    int val;
    struct node* left;
    struct node* right;
    int ht;
} node; */
 
int nht(node * root);
int height (node * root);
int bfactor (node * root);
node * newnode(int val);
node * insert(node * root, int val);
node * balance (node * root);
node * lr(node * root);
node * rr ( node * root);
 
 
node * newnode(int val)
{
        node * temp = (node *) malloc(sizeof(node));
        temp->val = val;
        temp->left = NULL;
        temp->right = NULL;
        temp->ht = 1;
        return temp;
}
 
int height(node * root)
{
        if(root)
                return root->ht;
        else
                return 0;
}
 
int nht(node * root)
{
        int x = height(root->right);
        int y = height(root->left);
        if (x>y)
                return x+1;
        else
                return y+1;
}
int bfactor(node * root)
{
        return height(root->left)-height(root->right);
}
 
node * rr ( node * root )
{
        node * x;
        x = root->left;
        root->left = x->right;
        x->right = root;
        root->ht = nht(root);
        x->ht = nht(x);
        return x;
}
 
node * lr ( node * root )
{
        node * x;
        x = root->right;
        root->right = x->left;
        x->left = root;
        root->ht = nht(root);
        x->ht = nht(x);
        return x;
}
 
node * insert (node * root, int val)
{
        if (!root)
                return newnode(val);
        else if (root->val < val)
                {
                        root->right= insert (root->right, val);
                }      
 
        else
                {
                        root->left= insert (root->left, val);
                }      
 
        root->ht = nht(root);
        int bal = bfactor(root);
        if(bal>1)
        {
                if (bfactor(root->left)>=0)
                        return rr(root);
                else
                {
                        root->left = lr(root->left);
                        return rr (root);
                }
        }
        if(bal<-1)
        {
                if (bfactor(root->right)<=0)
                        return lr(root);
                else
                {
                        root->right = rr(root->right);
                        return lr (root);
                }              
        }
        return root;
}

C Program to Defines the entry point for the console application

How to write a C Program to Defines the entry point for the console application in C Programming Language ?


Solution:


//Defines the entry point for the console application 
#include "stdio.h"


int main(int argc, char argv[])
{
  unsigned int x,y,p,n,ergebnis;
 printf("Bitte gegen sie die Zahl ein die Veraendert werden soll: ");
 scanf("%d", &x);
 printf("\nBitte geben sie die Zahl ein von der die zu Veraendernde Bits kommen sollen:");
 scanf("%d", &y);
 printf("\nBitte geben sie die Position der Bits ein von der an die Veraenderung stattfinden soll (Zaehlung beginnt rechts mit Bit 0): ");
 scanf("%d", &p);
 printf("\nBitte geben sie die Anzahl der Bits ein die veraendert werden sollen: ");
 scanf("%d", &n);
 ergebnis= ~y;
 ergebnis= ergebnis << (32-n);
 ergebnis= ergebnis >> (32-(p+1));
 ergebnis= ergebnis ^ x;
 ergebnis= ergebnis & x;
 printf("\n%d\n",ergebnis);
 
 char ch;
 scanf("%c",& ch);
 getchar();
 return 0;
}

Defines the entry point for the console application.

How to write a C program to Defines the entry point for the console application in C Programming Language ?

Solution:
//Defines the entry point for the console application.
//

#include "stdafx.h"
#define _CRT_SECURE_NO_WARNINGS

float somatorio(int N) {

float S = 0, termo=1;
for (int i = 1; i <= N; i++)
{
termo = termo * (2.0f / i);
S += termo;
}
return S;
}
//exercicio 6
float somatorio6(int N)
{
float soma = 1;
int sinal = -1;
for (int i = 1; i <= N; i++)
{
//soma = soma + 1.0f / (2 * i); //solucao com +

if (i % 2 == 0)
soma += 1 / (i * 2.0f);
else
soma -= 1 / (i * 2.0f);
/*
//Solucao 2
soma = soma + (1.0f / (2 * i))*sinal;
sinal = -sinal;*/
}
return soma;
}

//meia arvore de natal: Esquerda -> Direita
void meiaarvoreesquerda(int N)
{
for (int linha = 0; linha < N; linha++)
{
for (int ramo = 0; ramo <= linha; ramo++)
printf("%d",ramo+1);

printf("\n");
}

}
//Asterisco
void asterisco(int N)
{
for (int linha = 0; linha < N; linha++)
{
for (int ramo = 0; ramo <= linha; ramo++)
printf("*");

printf("\n");
}

}
//arvore direita para esquerda
//meia arvore de natal: direita -> esquerda
void meiaarvoredireita(int N)
{
for (int linha = 1; linha <= N; linha++)
{
for (int k = 0; k < N - linha; k++)
printf(" ");
for (int ramo = 0; ramo < linha; ramo++)
printf("*");

printf("\n");
}

}
//exercicio 10
float potencia(float a, int N)
{
float potencia = 1;
for (int i = 1; i <= N; i++)
potencia *= a;

return potencia;
}
//exercicio 9 : Capital Acumulado
float calculoCA(float CI, float j, int N)
{
return CI*potencia(1 + j, N);
}

//exercicio 11 B alterada
void mediaproduto()
{
int n = 0;
float media = 0.0, valor = 0.0, produto=1;

do {
printf("Valor:");
scanf("%f", &valor);
if (valor > 0) {
media += valor;
produto *= valor;
n++;
}
} while (valor > 0);
printf(" Media:%.3f produto:%.3f", media, produto);
printf("\n");
}
int main()
{
/*int N;
float j;
float CI;
printf("N:");
scanf("%d", &N);
printf("j:");
scanf("%f", &j);
printf("CI:");
scanf("%f", &CI);
//.3 = 3 casas decimais
printf("%.3f", calculoCA(CI,j,N));
printf("\n");
//meiaarvoredireita(N);
*/
mediaproduto();
return 0;
}