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

C++ program to input temperature in Fahrenheit and convert it to Celsius and Kelvin

/* Program will ask user to input temperature in  fahrenheit and 
*convert it to celcius and kelvin
*By: Destiny Muldrow
*/

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
  //set the variables (constant is used to convert to kelvin)  
  const double DIFFERENCE = 273.15;
  double fahrenheit;
  double celcius;
  double kelvin;
  
  //asks user for the temperature in fahrenhheit
  cout << "Enter temperature in fahrenheit: ";
  cin >> fahrenheit;
  
  //calculate how to convert fahrenheit to celcius and kelvin
  celcius = (fahrenheit - 32) / 1.8;
  kelvin = celcius + DIFFERENCE; 
  
  //sets the decimal place to three places over
  cout << fixed << setprecision(3);
  
  //output the temperature in celcius and kelvin
  cout << "Temperature in celsius is: " <<  celcius << endl;
  cout << "Temperature  in kelvin is: " << kelvin << endl;
   
  return 0;
}

VB.Net example to print Hello, world on the console

Module VBModule
 
    Sub Main()
        Console.WriteLine("Hello, world!")
    End Sub
  
End Module

Programmeringskonferens Gbg Uppgift 3 Avkastning

% Städa bort gamla variabler och grafer
clear
clf

maxforandring = 0.005;
startvarde = 2000;
ff_start = 1.02;
AntalIterationer=1000;
totalVarde=0;

% Upprepa simulering av 5-årsutveckling 1000 gånger 
for n=1:AntalIterationer
  varde = startvarde;
  ff_m = ff_start ^ (1/12);

  % Simulera en 5-årsutveckling (60 månader) 
  for m=1:60
  varde = varde * ff_m;

  % Beräkna ny månadsutveckling baserad på
  % ändrad årsutveckling
  ff_y = ff_m ^ 12;
  ff_y = ff_y + maxforandring * (2 * rand() - 1);
  ff_m = ff_y ^ (1/12);
  end

  % Spara slutvärdet efter 5 år, avrundat till 10-tal
  % Spara i en vektor för att kunna rita histogram
  varde_vektor(n)=round(varde / 10) * 10;
  
  % Spara totalvärdet för att kunna räkna genomsnitt
  totalVarde=totalVarde + round(varde / 10) * 10;
end

% Matlab-specifika kommandon för att analysera data
% Skapa ett histogram
%hist(varde_vektor,30)

# Bestäm antalet simuleringar som gav en minskning och räkna ut sannolikheten
antalMinskande=0;
for m =varde_vektor
    if m <= startvarde
      antalMinskande=antalMinskande+1;
    end
end
disp(["Genomsnittligt värde: ",num2str(mean(varde_vektor))])
disp(["Sannolikhet att det minskar: ",num2str(antalMinskande/length(varde_vektor))])

Programmeringskonferens Gbg Uppgift 1 3x+1 problemet

% Städa bort gamla variabler och grafer
clear
clf

antalTal=10000; % Variabel som anger antal tal
maxSteg=0;      % Variabel för att räkna antal steg

% k räknar från 0 till antalTal
for k=1:antalTal
    
  % Sätt startvärde för n, nollställ räknaren för steg 
  n=k;
  steg = 0;
  
  # Utför de steg som algoritmen föreskriver
  while n != 1
    steg =steg + 1;
    if n / 2 == round(n / 2)
      n=n/2;
    else
      n=3*n+1;
    end
  end

  if steg> maxSteg
      maxSteg=steg;
  end
  
  % Skriv ut resultat
  disp([num2str(k)," (kräver ",num2str(steg)," steg)"])
  stegArray(k)=steg;
end

%plot(1:antalTal,stegArray)
%[max maxindex]=max(stegArray)
maxSteg

Arm Assemb prac for me as owner of dis prac

asm

/* -- first.s */
/* This is a comment */
.global main /* 'main' is our entry point and must be global */
 
main:          /* This is main */
    mov r0, #2 /* Put a 2 inside the register r0 */
    bx lr      /* Return from main */

PHP array example to perform operations, sorting array and printing its first element

php

<?php
$arr = array(
    '2' => 0,
    '3' => 0,
    '4' => 0,
    '5' => 0
);
for ($i = 1; $i <= 120; $i++) {
    $sum = 0;
    for ($j = 1; $j <= $i; $j++) {
        $sum += $j;
    }
    if ($sum % 2 == 0) {
        $arr[2]++;
    }
    if ($sum % 3 == 0) {
        $arr[3]++;
    }
    if ($sum % 4 == 0) {
        $arr[4]++;
    }
    if ($sum % 5 == 0) {
        $arr[5]++;
    }
}
asort($arr);
$arr = array_keys($arr);
echo 'answer: ' . $arr[0];
?>

C example to implement functions for arithmetic operations

c

/*
 * math.h
 *
 *  Created on: Aug 22, 2017
 * 	Last Edited: Sept 5, 2017
 *      Author: Russell Trafford
 */

/* Your assignment is to take the math function and implement at least the following functions:
 * + Add (num1 + num2)
 * - Subtract (num1 - num2)
 * * Multiply (num1 * num2)
 * / Divide (num1 / num2)
 * % Modulus (num1 % num2)
*/

#include <stdio.h>
#ifndef MATH_H_
#define MATH_H_

int math(int num1, int num2, char Operator);

int main()
{

    int num1 = 4;
    int num2 = 2;
    char Operator = '%';
    int ret;

    ret = math(num1, num2, Operator);

    printf("Answer is : %d\n", ret);

    return 0;
}

//Part of your documentation should be listing the valid inputs and outputs for the functions you create.
int math(int num1, int num2, char Operator)
{
    int result;

    if (Operator == '+') {
        result = num1 + num2;
    }
    else if (Operator == '-') {
        result = num1 - num2;
    }
    else if (Operator == '*') {
        result = num1 * num2;
    }
    else if (Operator == '/') {
        result = num1 / num2;
    }
    else if (Operator == '%') {
        result = num1 % num2;
    }

    return result;
}

#endif /* MATH_H_ */

C++ template example to find max area and perimeter of rectangles

cpp

#include <iostream>
#include <functional>
#include <stdio.h>      
#include <math.h>       
#include <string.h>
#include <vector>
#include <stdlib.h>     
using namespace std;

// Generic findMax, with a function object, C++ style.
// Precondition: a.size( ) > 0.
template <typename Object, typename Comparator>
const Object & findMax( const vector<Object> & arr, Comparator isLessThan )
{
    int maxIndex = 0;

    for( int i = 1; i < arr.size( ); ++i )
        if( isLessThan( arr[ maxIndex ], arr[ i]))
            maxIndex = i;
    return arr[ maxIndex ];
}

// Generic findMax, using default ordering.
template <typename Object>
const Object & findMax( const vector<Object> & arr )
{
    return findMax( arr, less<Object>{ } );
}

class Rectangle 
{
    /* 
        going to assume I can just make a rectangle in a 
        0,y2--------x2,y2
          |            |
        0,0--------x2,0 style.
        using distance forumla and stuff could also be used
        but since rectangles are 90 angels all around
        I'll will do it with the assumption that it is a nice rectangle
        and is not rotated in some form, since this would be a general answer
        if there is rotation a small change would work
    */
    private:
        int x1, y1, x2, y2; 
        int getLength (int start, int end)
            {return abs(start-end); }
        int getWidth (int start, int end)
            {return abs(start-end);}
        int Length = getLength(x1,x2);
        int Width = getWidth(y1,y2);
    public:
        void set_cords (int cord1, int cord2, int cord3, int cord4) 
        {
            x1 = cord1, 
            y1 = cord2, 
            x2 = cord3, 
            y2 = cord4;
        }
        void printStuff () 
        {
            printf ("%d \n", x1);
            printf ("%d \n", y1);
            printf ("%d \n", x2);
            printf ("%d \n \n", y2);
            printf ("%d \n", getLength(x1,x2));
            printf ("%d \n \n", getWidth(y1,y2));
            printf ("%d \n", Area());
            printf ("%d \n \n", Perimeter());
            
        }
        int Area() {return getLength(x1,x2)*getWidth(y1,y2);}
        int Perimeter() {return getLength(x1,x2)*2 + getWidth(y1,y2)*2;}
        /* 
        For some reason Length and Width give tons of problems not sure how to fix
        it so just going to use the step use to get the Length and Width  
        int Area(){return Length*Width;}
        int Perimeter() {return Length*2 + Width*2;}
        
        Must be something with PBR and PBV but not sure.
        */
        
        /*  no idea what this is for 
        bool operator( )( const int & lhs, const int & rhs ) const
        { return strcasecmp( lhs.c_str( ), rhs.c_str( ) ) < 0; }
        */       
};

int main () {
    int myints1[] = {0,0,5,3};
    int myints2[] = {0,0,3,9};
    int myints3[] = {0,0,5,25};
    int myints4[] = {0,0,10,13};
    Rectangle Rect1;
    Rectangle Rect2;
    Rectangle Rect3;
    Rectangle Rect4;
    Rect1.set_cords(myints1[0], myints1[1], myints1[2], myints1[3] );
    Rect2.set_cords(myints2[0], myints2[1], myints2[2], myints2[3] );
    Rect3.set_cords(myints3[0], myints3[1], myints3[2], myints3[3] );
    Rect4.set_cords(myints4[0], myints4[1], myints4[2], myints4[3] );
    /*  Can be use to check stuff in more detail if needed  
        Rect1.printStuff();
        Rect2.printStuff();
        Rect3.printStuff();
        Rect4.printStuff();
    */
    int myAreas[] = {Rect1.Area(), Rect2.Area(), Rect3.Area(), Rect4.Area()};
    int myPerimeters[] = {Rect1.Perimeter(), Rect2.Perimeter(), Rect3.Perimeter(), Rect4.Perimeter()};
  
    vector<int> Areas (myAreas, myAreas + sizeof(myAreas) / sizeof(int) );
    vector<int> Perimeters (myPerimeters, myPerimeters + sizeof(myPerimeters) / sizeof(int) );
    /*
    figure 1.25 use a lot of vectors for the max instead of using 
    the arrays.  The question ask for usage of array, but is kind of
    the same thing so, I just did it like so (?)
    Also no idea whatsoever on what 
    template <typename Object, typename Comparator>
    const Object & findMax( const vector<Object> & arr, Comparator isLessThan )
    stuff is about can't understand almost anything of that code portion.
    */
    printf ("%s %d \n", "the max area in these rectangels is: ", findMax(Areas));
    printf ("%s %d \n", "the max primeter in these rectangels is: ", findMax(Perimeters));
    
    /*
    Not Sure if I had to make something else, like point which rectangle is it or
     I have ro give a name to each rectangle and then after finding the one with the
     max area do a for loop to search which rectangle it was that had that area
     or something else, but to do that, it feels like it would require a lot more code
     than what it is asking for, or make the areas into vector of arrays where
     each array has a form {RectName, Area, Perimete} and then do some stuff with this
     but oing so means you would somewhat had to mess with the findMax to make it work
     but findMax feels according to the question feels like we shouldn't mess with.
     So I just print the result of which area and perimeter are the biggest.
    */
    return 0;
}

Python example to read a file with exception handling

import sys

def getfilen():
    global fname
    fname = sys.ergv[1]
def getfileninp():
    global fname
    fname = raw_input("File Name: ")
try:
    getfilen()
except:
    getfileninp()
try:
    f = open(fname, "r")
except:
    print("\nBad Filename / Binary File")
    sys.exit()
linen = 1
for line in f:
    print("1: %s %s %s" % linen, fname, line)
    linen = linen + 1
print("EOF")

fork() example in C language

c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main()
{
    int a = 1000;
    printf("\nAgain ...%d", (int)getpid());
    if (fork() == 0) {
        printf("\nIn child ...");
        a /= 2;
        printf("\nValue of a = %d. PID: %d", a, (int)getpid());
    }
    else {
        printf("\nIn parent ...");
        if (fork()) {
            printf("\nSecond fork() ... PID: %d", (int)getpid());
            a *= 2;
            printf("\nValue of a = %d. PID: %d", a, (int)getpid());
            if (execlp("ls", "ls", 0) == -1) {
                a = a + 2;
                printf("\nValue of a = %d. PID: %d", a, (int)getpid());
            }
        }
        else {
            printf("\nIncrement ...");
            a += 2;
            printf("\nValue of a = %d. PID: %d", a, (int)getpid());
        }
    }
    a++;
    printf("\nValue of a = %d. PID: %d", a, (int)getpid());
    printf("\nExiting %d\n", getpid());
    return 0;
}

Advertisements
Loading...

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