Monday, November 13, 2017

Add/Sum two numbers using pointers in C/C++ Language

1 comments

Problem

Take two integer inputs from user. Create Sum function which should receive two integers using pointers. Calculate the sum and display the result in main function.

Code

#include "stdafx.h"
#include <iostream>
#include<conio.h>
using namespace std;


int sum (int *, int *);

int main()
{
int a,b,c;

cout << "Enter 1st number: ";
cin >> a;
cout<<"enter 2nd number: ";
cin >> b;

c = sum (&a,&b);

cout << "Sum = "<< c << endl;

cout<<"Press any key to close.";

getch();

return 0;
}

int sum (int *a, int *b)
{
int c;
c = (*a + *b);
return c;
}

Output

Add/Sum two numbers using pointers in C/C++ Language
Add/Sum two numbers using pointers in C/C++ Language

Read More..

Saturday, November 4, 2017

Calculate Square of 3 input integers using function and pointers

0 comments

Problem

Write a program to calculate square of an integer, input by the user. Declare a function with the name of square and should receive 3 integers. The function should then calculate the square of these three integers and return the square. Display the result.

Code

#include "stdafx.h"
#include <iostream>
#include<conio.h>
using namespace std;

void square (int *, int *, int *);

int main()
{
int a,b,c;

cout << "Enter 1st number: ";
cin >> a;

cout << "Enter 2nd number: ";
cin >> b;

cout << "Enter 3rd number: ";
cin >> c;

square (&a, &b, &c);

cout << "Square of Ist number = "<< a;
cout << "\nSquare of 2nd number = "<< b;
cout << "\nSquare of 3rd number = "<< c << endl ;


cout<<"Press any key to close.";

getch();

return 0;
}

void square (int *a, int *b, int *c)
{
*a = (*a * *a);
*b = (*b * *b);
*c = (*c * *c);
}

Output

Calculate Square of 3 input integers using function and pointers
Calculate Square of 3 input integers using function and pointers

Read More..

Sunday, October 15, 2017

Display Add/Sum of two integers in C Language

0 comments

Problem

Write a program in C language which takes two integers as input from user. Write a function to add these two integers and store the sum in another integer. Display the result.

Code

// lab02..cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include<conio.h>
using namespace std;


int sum (int, int);

int main()
{
int a,b,c;

cout << "Enter 1st number: ";
cin >> a;
cout<<"enter 2nd number: ";
cin >> b;

c = sum (a,b);

cout << "Sum = "<< c << endl;

cout<<"Press any key to close.";

getch();

return 0;
}

int sum (int a, int b)
{
int c;
c = a+b;
return c;
}

Output

Display Add/Sum of two integers in C Language
Display Add/Sum of two integers in C Language

Read More..

Saturday, October 7, 2017

Date Sum Using Structure in C/C++ Language

0 comments

Problem

Create a struct called data. User should be able to input day, month, and year. Take two date inputs and add them. Display the result.

Code

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

//struct decleration
struct data {
int years;
int months;
int days;
}d1,d2;

//function protorypes
void add_data();
void add_dates();
void display(int ,int ,int);

int main()
{
    //function calls
    add_data();
    add_dates();
   
printf ("\n\nPress any key to exit.");
getch();
return 0;
}

//adding record
void add_data()
{
     //1st date
     printf ("Enter 1st date.");
   
     printf ("\n\nEnter year: ");
     scanf ("%d",&d1.years);
   
     printf ("\nEnter month: ");
     scanf ("%d",&d1.months);
   
     printf ("\nEnter day: ");
     scanf ("%d",&d1.days);
   
     display(d1.days,d1.months,d1.years);
     //printf ("\n\nThe 1st date is %d/%d/%d",d1.days,d1.months,d1.years);
   
     //Second time
     printf ("\n\n\nNow, please type the 2nd date.");
   
     printf ("\nEnter year: ");
     scanf ("%d",&d2.years);
   
     printf ("\nEnter month: ");
     scanf ("%d",&d2.months);
   
     printf ("\nEnter day: ");
     scanf ("%d",&d2.days);
   
     display(d2.days,d2.months,d2.years);
}

void display (int a,int b,int c)
{
     printf("\nThe date is %d/%d/%d \n\n", a,b,c);
}   

void add_dates()
{
     int y,m,d;
   
     y = d1.years + d2.years;
     m = d1.months + d2.months;
     d = d1.days + d2.days;
   
     printf ("\n\n\nSum of the date's is %d/%d/%d",d,m,y);
}

Output

Date Sum Using Structure in C/C++ Language
Date Sum Using Structure in C/C++ Language

Read More..

Sunday, October 1, 2017

Time Sum Using Structure in C/C++ Language

1 comments

Problem

Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This can be in 12:59:59 format, or each number can be entered at a separate prompt (“Enter hours:”, and so forth). The program should then store the time in a variable of type struct time, and finally print out the total number of seconds represented by this time value:
long totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds

Code

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

struct time {
int hours;
int minutes;
int seconds;
};

//function prototype
void add (int,int,int,int,int,int);

int main()
{
    struct time t1,t2;
   
    //1st time
    printf ("Enter 1st time.");
   
printf ("\n\nEnter Hours: ");
scanf ("%d",&t1.hours);

printf ("\nEnter Minutes: ");
scanf ("%d",&t1.minutes);

printf ("\nEnter Seconds: ");
scanf ("%d",&t1.seconds);

    printf ("\n\nThe Time is %d:%d:%d",t1.hours,t1.minutes,t1.seconds);
   
    //Second time
    printf ("\n\n\nNow, please type the 2nd time.");
   
    printf ("\n\nEnter Hours: ");
scanf ("%d",&t2.hours);

printf ("\nEnter Minutes: ");
scanf ("%d",&t2.minutes);

printf ("\nEnter Seconds: ");
scanf ("%d",&t2.seconds);

    printf ("\n\nThe Time is %d:%d:%d",t2.hours,t2.minutes,t2.seconds);
   
    //function call
    add (t1.hours,t1.minutes,t1.seconds,t2.hours,t2.minutes,t2.seconds);
   
printf ("\n\nPress any key to exit.");
getch();
return 0;
}

//function definition
void add (int x,int y,int z,int a,int b,int c)
{
     int h,m,s;
   
     h = x + a;
     m = y + b;
     s = z + c;
   
     printf ("\n\n\nSum of the two time's is %d:%d:%d",h,m,s);
}

Code

Time Sum Using Structure in C/C++ Language
Time Sum Using Structure in C/C++ Language

Read More..

Sunday, September 24, 2017

Input Length and Breadth. Calculate Area and Perimeter Using Struct in C Language

0 comments

Problem:

Write a program which takes length and breadth as input. Than calculate area and perimeter of the room.

Code

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

struct distance{
int length;
int width;
};
struct room{
struct distance a;
}r;

void myftn();

int main()
{
myftn();


printf ("\n\nPress any key to exit.");
getch();
return 0;
}

void myftn ()
{
int p,s;

printf ("Enter lengtgh: ");
scanf ("%d",&r.a.length);
printf ("Enter widtgh: ");
scanf ("%d",&r.a.width);

p = 2 * (r.a.length * r.a.width);
s = (r.a.length * r.a.width);

printf ("\n\nArea = %d",s);
printf ("\nPerimeter = %d",p);
}

Output

Input Length and Breadth. Calculate Area and Perimeter Using Struct in C Language
Input Length and Breadth. Calculate Area and Perimeter Using Struct in C Language



Read More..

Sunday, September 17, 2017

Convert Fahrenheit to Celsius | Convert Celsius to Fahrenheit

0 comments

Problem

Write two functions in a program of C language to convert:

  • Function 1: Fahrenheit to Celsius
  • Function 2: Celsius to Fahrenheit

Code


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

void ftoc();
void ctof();

int main()
{
ftoc();
        ctof();
    
printf ("\n\nPress any key to exit.");
getch();
return 0;
}

void ftoc()
{
float f,c;
printf ("Enter temperature in Fahrenheit: ");
scanf ("%f",&f);

c = 5.0 / 9.0 * (f-32.0);

printf ("Temperature in celsius: %f",c);
}

void ctof()
{
float f,c;
printf ("\n\nEnter temperature in celsius: ");
scanf ("%f",&c);

f = ((9.0 / 5.0 * c) + 32.0);

printf (" Temperature in celsius: %f",f);
}

Output

Convert Fahrenheit to Celsius | Convert Celsius to Fahrenheit
Convert Fahrenheit to Celsius | Convert Celsius to Fahrenheit

Read More..

Friday, October 7, 2016

Linear Search and Binary Search using C and C++ Language - recursive

0 comments
Write programs in C/C++ language to find a specific number from a user input array elements by following linear search and binary search algorithms.

Method – 1: Linear search

Code

#include<conio.h>
#include<iostream>
using namespace std;

int main()
{
                    //declaring variables and array
    int max = 5;
    int i,j=0,s;
    int a[max];
                    //Taking input(Numbers) from user.
    for(i=0;i<max;i++)
    {
        cout<<"Enter "<<(i+1)<<"th Number: ";
        cin>>a[i];
    }
                    //Asking user to enter the number he/she wants to search
    cout<<"\nEnter number to search: ";
    cin>>s;
                    //Searching the number
    cout<<"\nAfter Searching:";
    for(i=0;i<max;i++)
    {
        if (a[i]==s)
        {
           cout<<"\n\tThe number ("<<a[i]<<") is at "<<(i+1)<<"th position.";
           j = 1;
        }
    }
   
                   //Display Number and its position (after searching)
    if (j == 0)
          cout<<"Entered number is not in the list.";
   
    cout<<"\n\nPress any key to exit.";
    getch();
    return 0;  
}

Output

Linear Search c/c++

Method – 2: Recursive linear search

Code

#include <iostream>
#include <conio.h>
using namespace std;

int linearSearch( const int array[], int length, int value);

int main()
{
    const int arraySize = 100;
    int a[arraySize];
    int element;

    for( int i = 0; i < arraySize; i++)
    {
         a[i] = 2 * i;
    }
   
    element = linearSearch( a, arraySize, 8);

    if( element != -1 )
        cout << "Found value in element " << element << endl;

    else
        cout << "Value not found." << endl;

    getch();
    return 0;
}

int linearSearch( const int array[], int length, int value)
{
    if(length==0)
        return -1;
    else if(array[length-1]==value)
        return length-1;
    else return linearSearch( array, length-1, value);
}

Output

Recursive Linear Search c/c++

Method – 3: Binary Search

Code

#include<conio.h>
#include<iostream>
using namespace std;

int main()
{
                    //declaring variables and array
    int max = 10;
    int i,j,s,m,location,k;
    int a[max];
                    //Taking input(Numbers) from user.
    for(i=0;i<max;i++)
    {
        cout<<"Enter "<<(i+1)<<"th Number: ";
        cin>>a[i];
    }
                    //Asking user to enter the number he/she wants to search
    cout<<"\nEnter number to search: ";
    cin>>s;
                    //Searching the number
    i = 0;
    j = max;
    while (i < j)
    {
          m = ((i+j)/2);
          if (s > a[m])
             i = m+1;
          else
              j = m;
    }
   
    if (s == a[i])
    {             //if searched number is equal to a[i]
       cout<<"\n\nEntered number ("<<s<<") is at "<<(i+1)<<"th position.";
    }
    else
    {             //if the searched number is not in the list
        cout<<"\n\nEntered number is not in the list.";
    }
   
    cout<<"\n\nPress any key to exit.";
    getch();
    return 0;  
}

Output

Binary Search c/c++

Read More..

Find the nth Fibonacci number in C/C++ Language

1 comments
There are multiple ways to achieve that. Following are the 3 methods to find nth Fibonacci number.
Algorithm: We know that
Fib (n) = fib (n-1) + fib (n-2)
Our program will stop if one of the following conditions are met:
n = 2 || n = 1

Method – 1: iteratively using for loop

Code

#include<iostream>
#include<conio.h>
using namespace std;

int fib(int n);

int main()
{

      int n, answer;
      cout << "Enter number to find: ";
      cin >> n;

      cout << "\n\n";

      answer = fib(n);

      cout << answer << " is the " << n << "th Fibonacci number\n";
      getch();
      return 0;
}

int fib(int n)
{
  int u = 0;
  int v = 1;
  int i, t;

  for(i = 2; i <= n; i++)
  {
    t = u + v;
    u = v;
    v = t;
  }
  return v;
}

Output


Method – 2: Using recursion

Code

#include<iostream>
#include<conio.h>
using namespace std;

int fib(int n);

int main()
{

      int n, answer;
      cout << "Enter number to find: ";
      cin >> n;

      cout << "\n\n";

      answer = fib(n);

      cout << answer << " is the " << n << "th Fibonacci number\n";
      getch();
      return 0;
}

int fib (int n)
{
      cout << "Processing fib(" << n << ")... ";

      if (n < 3 )
      {
         cout << "Return 1!\n";
         return (1);
      }
     
      else
      {
        cout << "Call fib(" << n-2 << ") and fib(" << n-1 << ").\n";
        return( fib(n-2) + fib(n-1));
      }
}

Output


Method – 3: Using recursion

Code

#include<iostream>
#include<conio.h>
using namespace std;

int fib(int n);

int main()
{

      int n, answer;
      cout << "Enter number to find: ";
      cin >> n;

      cout << "\n\n";

      answer = fib(n);

      cout << answer << " is the " << n << "th Fibonacci number\n";
      getch();
      return 0;
}
int fib(int n)
{
  if (n < 2)
    return n;
  else
    return fib(n-1) + fib(n-2);
}

Output


Read More..

Wednesday, October 5, 2016

Write a simulator for the assembly language ISA design in C language.

0 comments

Code

#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

                                                                                                //initializing the memory and registers
void initialization(int a[]){
               
                for(int i=0;i<16;i++)
                                a[i] = (i+1)*10;
}

                                                                                                //number of instructions
int input_instructions(){
               
                int x;
               
                printf("Enter the number of instructions you want to perform: ");
                scanf_s ("%d",&x);

                return x;
}

                                                                                                //loading the register from memory
void load(int row, int inst[][3]){
               
                inst[row][0] = 1;

                printf("Enter the number of register ranging from 1 to 16: ");
                scanf_s("%d",&inst[row][1]);
               
                printf("Enter the number of memory from which you want to load ranging from 1 to 16: ");
                scanf_s("%d",&inst[row][2]);

}

                                                                                                //adding the two registers
void add(int row,int inst[][3]){

                inst[row][0] = 5;

                printf("Enter the destination register number: ");
                scanf_s("%d",&inst[row][1]);

                printf("Enter the source register number: ");
                scanf_s("%d",&inst[row][2]);
               
}

                                                                                                //storing the value to the memory
void store(int row,int inst[][3]){
               
                inst[row][0] = 3;

                printf("Enter the register number: ");
                scanf_s("%d",&inst[row][1]);

                printf("Enter the memory number: ");
                scanf_s("%d",&inst[row][2]);
               
}

                                                                                                //display the instruction memory
void display_pattern(int no_of_ins,int inst[][3]){
                system("cls");
                printf("\********************************************************************************");
                printf("\n****************************** Instruction Memory ******************************");
                printf("\n********************************************************************************");

                for(int i=0;i<no_of_ins;i++){
                               
                                printf("\n\t\t\t\t%d\t%d\t%d\n",inst[i][0],inst[i][1],inst[i][2]);
                }
                printf("\n\n");
                for(int i=0;i<no_of_ins;i++){
                               
                                if(inst[i][0] == 1){
                                               
                                                printf("\t\t\t\tLoadR\tR%d\tM%d\n\n",inst[i][1],inst[i][2]);

                                }
                                else if(inst[i][0] == 3){
                                               
                                                printf("\t\t\t\tStoreR\tR%d\tM%d\n\n",inst[i][1],inst[i][2]);

                                }

                                else if(inst[i][0] == 5){
                                               
                                                printf("\t\t\t\tAddR\tR%d\tR%d\n\n",inst[i][1],inst[i][2]);

                                }
                }
                printf("\n\n\n\nPress any key to continue...");
                _getch();
}

                                                                                                //display registers and memorys
void display_reg_mem(int reg[],int mem[]){
                printf("\n\nRegister: ");
                                                for(int j=0;j<16;j++)
                                                                printf("%d   ",reg[j]);
                printf("\n-----------------------------------------------------------------------------");
                printf("\nMemory: ");
                                for(int j=0;j<16;j++)
                                                printf("%d  ",mem[j]);
}

                                                                                                //ISA simulator
void display_cycles(int no_of_inst,int inst[][3],int reg[],int mem[]){

                for(int i=0;i<no_of_inst;i++){

                                system("cls");
                                printf("********************************************************************************");
                                printf("\n****************************** ISA Simulator ***********************************");
                                printf("\n********************************************************************************");

                                printf("\n\n\n\n\n******************************* Instruction # %d ********************************",(i+1));
               
                                printf("\n\n\n\n\nFetch Instruction:\t %d\t%d\t%d\n",inst[i][0],inst[i][1],inst[i][2]);
                                display_reg_mem(reg,mem);

                                if(inst[i][0] == 1){

                                                printf("\n\n\n\n\nDecode Instruction:\t LoadR\tRegister%d\tMemory%d\n",inst[i][1],inst[i][2]);
                                                display_reg_mem(reg,mem);
                                               
                                                printf("\n\n\n\n\nExecute Instruction:\t Register updated Succesfully\n");
                                               
                                                reg[(inst[i][1]-1)] = mem[(inst[i][2]-1)];

                                                display_reg_mem(reg,mem);
                                }

                                if(inst[i][0] == 5){

                                                printf("\n\n\n\n\nDecode Instruction:\t AddR\tRegister%d\tRegister%d\n",inst[i][1],inst[i][2]);
                                                display_reg_mem(reg,mem);
                                               
                                                printf("\n\n\n\n\nExecute Instruction:\t Register updated Succesfully\n");
                                               
                                                reg[(inst[i][1]-1)] = reg[(inst[i][1]-1)] + reg[(inst[i][2]-1)];
                                               
                                                display_reg_mem(reg,mem);
                                }

                                if(inst[i][0] == 3){

                                                printf("\n\n\n\n\nDecode Instruction:\t StoreR\tRegister%d\tMemory%d\n",inst[i][1],inst[i][2]);
                                                display_reg_mem(reg,mem);

                                                printf("\n\n\n\n\nExecute Instruction:\t Memory updated Succesfully\n");
                                               
                                                mem[(inst[i][2]-1)] = reg[(inst[i][1]-1)];
                                               
                                                display_reg_mem(reg,mem);

                                }

                                printf("\n\n\n\nPress any key to continue...");
                                _getch();
                }
}


int main(){
                system("color 3f");
               
                int choice,count=0,row=0,lines;

                int registers[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
                int memorys[16];
               
                initialization(memorys);

                lines = input_instructions();
               
                int instructions[100][3];

                for(int i=0;i<lines;i++){                      
                                system("cls");
                                printf("Select one of the following instruction type from the following:\n");
                                printf("\t1. Load  \n");
                                printf("\t2. Add   \n");
                                printf("\t3. Store \n");
                                printf("\t4. Exit  \n\n");

                                printf("Enter your choice: ");
                                choice = _getche();
                               
                                switch (choice)
                                {
                                               
                                                case '1': //load
                                                                system("cls");
                                                                load(row,instructions);                       
                                                                row++;
                                                                break;
                                                               
                                                case '2': //add
                                                                system("cls");
                                                                add(row,instructions);
                                                                row++;
                                                                break;
                                                               
                                                case '3': //store
                                                                system("cls");
                                                                store(row,instructions);
                                                                row++;
                                                                break;
                                               
                                                case '4': //exit
                                                                exit(0);
                                               
                                                default:
                                                                system("cls");
                                                                printf("\n\nInvalid input! Press any key to continue.");
                                                                break;
                                }
                }
                system("cls");
                display_pattern(lines,instructions);

                system("cls");
                display_cycles(lines,instructions,registers,memorys);

                exit(0);
                return 0;
}



Program Output - Screen Shots

Screen 01
In the above screen 01 user must enter the instructions to run the program. For example we want to run our code for 4 instructions, so we will write 4 and hit enter.
The following screen will be displayed.
Screen 02
There are 4 options 1st option is used to load the register from memory, 2nd option add the two registers, 3rd option stores the value from register to the memory, and the 4th option is used to quit the program. For example, we press 1 and hit enter.
The following screen will be displayed.
Screen 03
User will be asked to enter the register number and the memory number. We will enter 2 for register and 7 for memory.
Screen 04
Same procedure will be followed again i.e. steps for screen 02.

Screen 05
This time we will enter 3 for register and 6 for the memory.
After you have entered the value in the memory hit enter… the program will show you the following screen.
Screen 06
This time press 2 and hit enter.
Screen 07
It will ask you to enter two register numbers one by one. The 1st register is the destination and the 2nd register is the source i.e. after the value is entered, the value of the destination will be overwritten with the sum of the two registers in the add instruction. Here we have entered 2 and 3 register numbers where 2nd register will be overwritten after the execution with the sum of the register no 2 and 3.
Screen 08
After you enter the register number, the above screen will be displayed again, press 3 this time and hit enter.
Screen 09
The above screen will be shown asking the user to enter the register number from which he/she wants to store data and then the program will ask for the memory number. For instance, we enter 2 for register and 6 for memory and press enter.
Screen 10
As, we have entered 4 instructions (2 load, 1 add and 1 store). The above screen will be displayed asking the user to press any key to continue if he/she have viewed the instructions. After we hit enter the following screen will be displayed.
Screen 11
The program will show the user ISA simulator with the 1st instruction. As, the 1st instruction was to load the register from the memory i.e. 1 2 7. After the execution the register’s value will be updated and as we can see in our example it has updated the 2nd register with the value 70 taken from the memory’s 7th location.
Let’s have a closer look at the above screen 11, we can see there are three steps involved are involved for each cycle (instruction).

1st step - the fetch instruction

It fetches the instruction from the memory and loads it into the processor.

2nd step - the decode instruction

After the 1st step the 2nd step takes place: It decodes the instruction after it has been fetched, so that the computer can perform its specific operation.

3rd step - execute instruction

It executes the instruction and updates the values according to the instruction, for instance in this instruction it will update the value of the 2nd register.
Screen 12
Similarly 2nd instruction will be displayed. It will update the 3rd register with the value 60.
Screen 13
The third instruction was to add the 2nd and 3rd register and answer have to be stored in the 2nd register as it was the destination register. Hence, we can see the value of the 2nd register have been updated to 130 from 70.
Screen 14
The last and 4th instruction that we entered was to store the result of add instruction from the 3rd instruction to the memory. In the above example we have stored the value of the 2nd register in the 6th memory location.

The user will be asked to press any key to continue and the program will be terminated as there were no more instructions to show.
Read More..

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact