Showing posts with label Triangle. Show all posts
Showing posts with label Triangle. Show all posts

Waveform Generation (Triangle Wave) C Program

How to write a Waveform Generation (Triangle Wave) program in C Programming Language ?


  1. /*
  2.  * main.c
  3.  *
  4.  * Title: Assignment 4 Waveform Generation (Triangle Wave)
  5.  *
  6.  * Authors: Daniel Hodges & Omar Arriaga
  7.  *
  8.  * Description: Interfaces with the MCP4921 DAC via SPI to generate
  9.  * a 2Vpp triangle wave with 1VDC offset and a period of 20ms using timers.
  10.  *
  11.  */
  12. #include <msp430g2553.h>
  13. void Drive_DAC(unsigned int level);
  14. int ISRcounter = 0;
  15. int main(void)
  16. {
  17.   WDTCTL = WDTPW + WDTHOLD;          // Stop watchdog timer
  18.   // 16Mhz SMCLK
  19.   if (CALBC1_16MHZ==0xFF)            // If calibration constant erased
  20.   {
  21.     while(1);                        // do not load, trap CPU!!
  22.   }
  23.   DCOCTL = 0;                        // Select lowest DCOx and MODx settings
  24.   BCSCTL1 = CALBC1_16MHZ;            // Set range
  25.   DCOCTL = CALDCO_16MHZ;             // Set DCO step + modulation
  26.   // Init Ports
  27.   P1DIR |= BIT4;                     // Will use BIT4 to activate /CE on the DAC
  28.   P1SEL  = BIT7 + BIT5;              // These two lines dedicate P1.7 and P1.5
  29.   P1SEL2 = BIT7 + BIT5;              // for UCB0SIMO and UCB0CLK respectively
  30.   // SPI Setup
  31.   // clock inactive state = low,
  32.   // MSB first, 8-bit SPI master,
  33.   // 4-pin active Low STE, synchronous
  34.   //
  35.   // 4-bit mode disabled for now
  36.   UCB0CTL0 |= UCCKPL + UCMSB + UCMST + /* UCMODE_2 */ + UCSYNC;
  37.   UCB0CTL1 |= UCSSEL_2;               // UCB0 will use SMCLK as the basis for
  38.                                       // the SPI bit clock
  39.   UCB0CTL1 &= ~UCSWRST;               // **Initialize USCI state machine**
  40.                                       // SPI now Waiting for something to
  41.                                       // be placed in TXBUF.
  42.   // set up SMCLK timer
  43.   CCTL0 = CCIE;                       // CCR0 interrupt enabled
  44.   CCR0 = 98;
  45.   TACTL = TASSEL_2 + MC_2;            // SMCLK, contmode
  46.   _enable_interrupts();               // enable interrupts
  47.   while(1){
  48.         // if statement determines when
  49.         // to increment/decrement DAC output
  50.         if (ISRcounter <= 1638){                        // increments DAC output from 0 to 2V
  51.                 Drive_DAC(ISRcounter);
  52.         }
  53.         else if(ISRcounter > 1638 & ISRcounter < 3276){ // decrements DAC output from 2 to 0V
  54.             Drive_DAC(3276-ISRcounter);
  55.         }
  56.         else{                                           // resets ISRcounter back to 0 to start over again
  57.                 ISRcounter = 0;
  58.                 Drive_DAC(ISRcounter);
  59.         }
  60.   }
  61. }
  62. // Drives DAC through SPI; takes value from 0 to 4096
  63. void Drive_DAC(unsigned int level){
  64.   unsigned int DAC_Word = 0;
  65.   DAC_Word = (0x3000) | (level & 0x0FFF);   // 0x3000 sets DAC for Write
  66.                                             // to DAC, Gain = 1, /SHDN = 1
  67.                                             // and put 12-bit level value
  68.                                             // in low 12 bits.
  69.   P1OUT &= ~BIT4;                           // Clear P1.4 (drive /CS low on DAC)
  70.                                             // Using a port output to do this for now
  71.   UCB0TXBUF = (DAC_Word >> 8);              // Shift upper byte of DAC_Word
  72.                                             // 8-bits to right
  73.   while (!(IFG2 & UCB0TXIFG));              // USCI_A0 TX buffer ready?
  74.   UCB0TXBUF = (unsigned char)
  75.                        (DAC_Word & 0x00FF); // Transmit lower byte to DAC
  76.   while (!(IFG2 & UCB0TXIFG));              // USCI_A0 TX buffer ready?
  77.   P1OUT |= BIT4;                            // Set P1.4   (drive /CS high on DAC)
  78.   return;
  79. }
  80. // Timer A0 interrupt service routine
  81. #pragma vector=TIMER0_A0_VECTOR
  82. __interrupt void Timer_A (void)
  83. {
  84.   ISRcounter++;   // Increment ISRcounter
  85.   CCR0 += 98;     // Extend timer by 6.125us
  86. }

C Program To Show Pascal Triangle

How to write a C Program To Show Pascal Triangle in C Programming Language ?

Solution:

C Program To Show Pascal Triangle

#include<stdio.h>

long factorial(int);

main()
{
   int i, n, c;

   printf("Enter the number of rows you wish to see in pascal triangle\n");
   scanf("%d",&n);

   for ( i = 0 ; i < n ; i++ )
   {
      for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
         printf(" ");

      for( c = 0 ; c <= i ; c++ )
         printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));

      printf("\n");
   }

   return 0;
}

long factorial(int n)
{
   int c;
   long result = 1;

   for( c = 1 ; c <= n ; c++ )
         result = result*c;

   return ( result );
}

C Program to Print Floyd's Triangle

How to write a C Program Print Floyd's Triangle in C Programming Language ?


Solution:


#include<stdio.h>
#include<conio.h>

main()
{
   int n, i,  c, a = 1;

   printf("Enter the number of rows of Floyd's triangle to print\n");
   scanf("%d",&n);

   for ( i = 1 ; i <= n ; i++ )
   {
      for ( c = 1 ; c <= i ; c++ )
      {
         printf("%d ",a);
         a++;
      }
      printf("\n");
   }

   getch();
   return 0;
}

output

1

2 3

4 5 6

7 8 9 10

In Floyd's triangle nth row contains n numbers.

C Program Nested Loop (inverted Triangle) C Code

How to write a C Program Nested Loop (inverted Triangle) C Code in C Programming Language ?


Here is a C Code for Nested loop ( inverted Triangle) with Output.

Solution:

Triangle Wave in C Program


#include<stdio.h>
int main()
{
    long long int amp, fre, a,i,j,t,m,tc;

    scanf("%lld", &tc);

    for(t=1;t<=tc;t++)
    {
    scanf("%lld %lld", &amp, &fre);

    for(a=1;a<=fre;a++)
    {
        for(i=1; i<=amp; i++)
        {
          for(j=1; j<=i; j++)
            {
                printf("%lld", i);
            }
            printf("\n");
        }
        for(i=amp-1; i>=1; i--)
        {
            for(j=1; j<=i; j++)
            {
                printf("%lld", i);
            }
            if(t==tc&&a==fre&&i==1)
                break;
            printf("\n");
        }
        printf("\n");
    }
    }
    return 0;
}

C Program to calculate the sum of elements of upper triangle of a n*n matrix using DMA

How to write a C Program to calculate the sum of elements of upper triangle of a n*n matrix using Dynamic memory allocation in C Programming Language ?

Solution:
/*C Program to calculate the sum of elements of upper triangle of a n*n matrix using Dynamic Memory allocation*/
#include<stdio.h>
#include<conio.h>
  void main()
{
  int **ip,m,n;
  int sum=0,i=0,j=0;
  clrscr();
  printf("\nEnter the row and column: ");
  scanf("%d%d",&m,&n);
  ip=(int**)malloc(m*sizeof(int));
  for(i=0;i<m;i++)
  ip[i]=(int*)malloc(n*sizeof(int));
  printf("\n");
  printf("\nEnter the elements: ");
  for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
{
  scanf("%d",&ip[i][j]);
}
}
  printf("\nEntered elements are:\n");
  for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
{
  printf("\t%d",ip[i][j]);
}
  printf("\n");
}
  for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
{
  if(i<=j)
  sum=sum+ip[i][j];
}
}
  printf("\nSum upper triangle is = %d",sum);
  getch();
}

OUTPUT:

Enter the row and column:
3
3

Enter the elements:
1
2
3
4
5
6
7
8
9

Entered elements are:
      1       2       3
      4       5       6
      7       8       9

Sum upper triangle is = 26

Calculate sum of element of upper triangle of m*n matrix by using dynamic memory allocation

How to write a c program to calculate sum of element of upper triangle of m*n matrix by using dynamic memory allocation in C Programming Language ?



Solution:
/* write a c program to calculate sum of element of upper triangle of m*n matrix by using dynamic memory allocation */
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
int *a[20];
int i,j,m,n,sum=0,k,*p;
clrscr();
printf("\nenter the rows and coloumn :");
scanf("%d%d",&m,&n);
if(m==n)
{
printf("\nenter the element :\n");
for(i=0;i<m;i++)
{
a[i]=(int *)malloc(n * sizeof(int));
for(j=0;j<n;j++)
{
scanf("%d",a[i]+j);
}
}
k=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i<j)
{
*(p+k)=a[i][j];
k++;
}
}
}
printf("the lower triangle element are:\n");
for(i=0;i<m;i++)
{
printf("%d",*(p+i));
sum=sum+*(p+i);
}
printf("\nsum is :%d",sum);
}
else
printf("not possible");
getch();
}
/*
enter the rows and coloumn :3 3                                              
                                                                             
enter the element :                                                          
4 7 8                                                                        
3 6 9                                                                        
1 8 2                                                                        
the lower triangle element are:                                              
789                                                                          
sum is :24                                                                    
*/

Calculate sum of element of lower triangle of m*n matrix by using dynamic memory allocation

How to write a C program to calculate sum of element of lower triangle of m*n matrix by using dynamic memory allocation in C Programming Language ?


Solution:
/* C program to calculate sum of element of lower triangle of m*n matrix
by using dynamic memory allocation */
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
int *a[20];
int i,j,m,n,sum=0,k,*p;
clrscr();
printf("\nenter the rows and coloumn :");
scanf("%d%d",&m,&n);
if(m==n)
{
printf("\nenter the element :\n");
for(i=0;i<m;i++)
{
a[i]=(int *)malloc(n * sizeof(int));
for(j=0;j<n;j++)
{
scanf("%d",a[i]+j);
}
}
k=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
{
*(p+k)=a[i][j];
k++;
}
}
}
printf("the lower triangle element are:\n");
for(i=0;i<m;i++)
{
printf("%d",*(p+i));
sum=sum+*(p+i);
}
printf("\nsum is :%d",sum);
}
else
printf("not possible");
getch();
}
/*

enter the rows and coloumn :3 3

enter the element :
1 2 3
7 8 9
3 6 9
the lower triangle element are:
736
sum is :16
*/

Bitmap Triangle in C Program

How to write a C Program to Bitmap Triangle in C Programming language ?


#include <Windows.h>
#include <stdio.h>
#include <math.h>

#define sB 500 // nie zmieniac, działa dla wartości <= 504, chuj wie czemu
typedef unsigned long int ULint;

int main(){
        FILE *obraz;
        obraz = fopen("obraz.bmp", "wb");
       
ULint tlo[sB][sB]; // deklaracja tablicy pikseli

// RYSOWANIE TŁA
        for (int i = 0; i < sB; i++){ // kolumny
for (int j = 0; j < sB; j++) // wiersze
{
tlo[i][j] = 0xFF000340;
}
        }

// RYSOWANIE TRÓJKĄTA
for (int i = 0; i < sB; i++){ // kolumny
for (int j = i/2; j < sB-(i/2); j++) // wiersze
{
tlo[i][j] = 0xFFFFF77F;
}
        }

        BITMAPV4HEADER hdr2; // deklaracja drugiego nagłówka
        hdr2.bV4Size = 108; // rozmiar struktury - nie ruszać, bo jebnie
 
        hdr2.bV4Width = sB; // rozmiar X (long)
        hdr2.bV4Height = sB; // rozmiar Y (long)
 
        hdr2.bV4Planes = 1; // warstwy, nie zmieniać
        hdr2.bV4BitCount = 32; // ilosc bitow na piksel, nie zmieniać
        hdr2.bV4V4Compression = 3; // kompresja, nie zmieniać
 
        hdr2.bV4SizeImage = hdr2.bV4Width * hdr2.bV4Height * 4; // rozmiar pliku ywjsciowego
 
        hdr2.bV4YPelsPerMeter = 2835; // STAŁE
        hdr2.bV4XPelsPerMeter = 2835; // STAŁE
        hdr2.bV4ClrUsed = 0; // STAŁE
        hdr2.bV4ClrImportant = 0; // STAŁE
 
        hdr2.bV4RedMask = 0x00FF0000; // czerwona 00 FF 00 00
        hdr2.bV4GreenMask = 0x0000FF00; // zielona 00 00 FF 00
        hdr2.bV4BlueMask = 0x000000FF; // niebieska 00 00 00 FF
        hdr2.bV4AlphaMask = 0xFF000000; // alpha FF 00 00 00
 
        hdr2.bV4CSType = LCS_WINDOWS_COLOR_SPACE; // nie zmieniać
        // hdr2.bV4Endpoints = wtf? // o chuj tu chodzi?
        hdr2.bV4GammaRed = 0; // nie zmieniać
        hdr2.bV4GammaGreen = 0; // nie zmieniać
        hdr2.bV4GammaBlue = 0; // nie zmieniać
int size_BITMAPV4 = sizeof(hdr2.bV4Size)+ // tego tym bardziej nie ruszać!
sizeof(hdr2.bV4Width)+
sizeof(hdr2.bV4Height)+
sizeof(hdr2.bV4Planes)+
sizeof(hdr2.bV4BitCount)+
sizeof(hdr2.bV4V4Compression)+
sizeof(hdr2.bV4SizeImage)+
sizeof(hdr2.bV4YPelsPerMeter)+
sizeof(hdr2.bV4XPelsPerMeter)+
sizeof(hdr2.bV4ClrUsed)+
sizeof(hdr2.bV4ClrImportant)+
sizeof(hdr2.bV4RedMask)+
sizeof(hdr2.bV4GreenMask)+
sizeof(hdr2.bV4BlueMask)+
sizeof(hdr2.bV4AlphaMask)+
sizeof(hdr2.bV4CSType)+
sizeof(hdr2.bV4Endpoints)+
sizeof(hdr2.bV4GammaRed)+
sizeof(hdr2.bV4GammaGreen)+
sizeof(hdr2.bV4GammaBlue);


        BITMAPFILEHEADER hdr1; // deklaracja nagłówka!
        hdr1.bfType = 0x4D42; // sygnatura nagłówka - nie ruszaj, bo jebnie
        hdr1.bfReserved1 = 0;
        hdr1.bfReserved2 = 0;
        hdr1.bfOffBits = size_BITMAPV4; // rozmiar struktury, też jebnie jak tkniesz
        hdr1.bfSize = hdr1.bfOffBits + hdr2.bV4SizeImage;

int size_BFH = sizeof(hdr1.bfType)+ // tego tym bardziej nie ruszać!
sizeof(hdr1.bfReserved1)+
sizeof(hdr1.bfReserved2)+
sizeof(hdr1.bfOffBits)+
sizeof(hdr1.bfSize);

printf("Rozmiar struktury naglowka = %d\nRozmiar struktury DIB = %d\n", size_BFH, size_BITMAPV4); // napis tekstowy
      
        fwrite((void*)&hdr1, sizeof(hdr1), 1, obraz); // zapis nagłówka 1
        fwrite((void*)&hdr2, sizeof(hdr2), 1, obraz); // zapis nagłówka 2
        fwrite(tlo, sizeof(tlo), 1, obraz); // zapis tablicy pikseli
 
        fclose(obraz);
        return 0;
}

C Program to Rectangular Triangle Using Operator

How to write a c program to Rectangular Triangle using operator in  C Programming Language ?


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

int isRectangularTriangle (int a, int b, int c)
{
    return ((a>0)&&(b>0)&&(c>0)&&((a*a == b*b + c*c) || (b*b == a*a + c*c) || (c*c == b*b + a*a)));
}

int main()
{
    int A, B, C;
    printf("A=0, B=2, C=8, isRectangularTriangle: %d\n\n", isRectangularTriangle(0, 2, 8));
    printf("A=1, B=-2, C=3, isRectangularTriangle: %d\n\n", isRectangularTriangle(1, -2, 3));
    printf("A=3, B=-4, C=5, isRectangularTriangle: %d\n\n", isRectangularTriangle(3, -4, 5));
    printf("A=3, B=4, C=5, isRectangularTriangle: %d\n\n", isRectangularTriangle(3, 4, 5));

    printf("input A\n");
    scanf("%d", &A);
    printf("input B\n");
    scanf("%d", &B);
    printf("input C\n");
    scanf("%d", &C);
    printf("isRectangularTriangle: %d\n", isRectangularTriangle(A, B, C));
    return 0;
}