-->





Translate

Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts

Cpp tutorial detecting a keypress and ASCII code

6 comments
Write a simple cpp program which detects a key pressed and its ASCII value with out pressing enter key and using cin>> on press ESC key program should exit. When user press a key its ASCII value should be displayed instantly. You can use any Cpp compiler CodeBlocks Recommended.

Concept used
  • Char and int data types
  • while infinite loop
  • conio.h header file getch() function
Program Logic and Explanation
  •  Its a simple program which reads a character of char data type
  • A header file conio.h is included to use getch() function
  • An infinite while loop breaks on pressing ESC key
  • In while loop  when program runs getch() function calls which wait to get character when we enter a character it store is ASCII value in int variable then displays the pressed key and its ASCII value on screen and then wait to another input a character.
This kind of program help user in game programming and programmer can control the value of any keypress by using a simple if statement. e.g you are making a game and want to control your inpute on pressing up, down, right, left, arrow keys simply get their ASCII values, use a control structure and control your input.
                          Value
Up Key               72
Down Key          80
Right Key          77
Left Key             75


Cpp code: Compiler used CodeBlocks Cpp Compiler

  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
  4. int main()
  5. {
  6.     char key_press;
  7.     int ascii_value;
  8.     cout<<"\n\t\t\tPress Any Key To Check  Its ASCI Value\n\n\t\t\tPress ESC to EXIT\n\n\n";
  9.     while(1)
  10.     {
  11.     key_press=getch();
  12.     ascii_value=key_press;
  13.     if(ascii_value==27) // For ESC
  14.      break;
  15.    cout<<"\t\t\tKEY Pressed-> \" "<<key_press<<" \" Ascii Value =  "<<ascii_value<<"\n\n";
  16.     }
  17.     return 0;
  18. }
Sample input outputs
Detecting a keypress in Cpp code

Its a simple program which helps the beginner to control input keys.

Read More...

Cpp Tutorial to reverse an array code

2 comments
Write a program which takes some elements as input in integer array then pass this array to a function which reverse its elements using for loop and swapping. 
 Display the reversed array with index number. Loop should execute less than array size. Use any c compiler (codeBlocks recommended).

This tutorial contains the following concepts

Program explanation
  • Program declare an integer array of size five, initialize it using for loop
  • Pass size and array name to function 
  • Function uses for loop and swap array elements with in it
  • A for loop is used to display the final result

Compiler used: CodeBlocks C Compiler
 code:
  1. #include <iostream>
  2. using namespace std;
  3. void Reverse_Array(int array[],int size)
  4. {   int temp;
  5.     size--;
  6.     int loop_count=0;
  7.     for(int i=0;size>=i;size--,i++)
  8.     {
  9.         loop_count++;// Counts the iterations
  10.         temp=array[i];
  11.         array[i]=array[size];
  12.         array[size]=temp;
  13.     }
  14.     cout<<"Number of Iterations: "<<loop_count<<endl;
  15. }
  16. int main()
  17. {
  18.     int array[5],i;
  19.     cout<<"\nEnter 5 Integer Values in Array\n"<<endl;
  20.     for(i=0;i<5;i++)
  21.     {
  22.     cout<<"Enter Value For Index Number array [ "<<i<<" ] -> ";
  23.     cin>>array[i];
  24.     }
  25.     // Calling Reverse Array Values Function
  26.     Reverse_Array(array,5);
  27.     cout << "\n\n\t\t\tReversed Array Values" << endl;
  28.     for(i=0;i<=4;i++)
  29.         {
  30.          cout<<"\t\t\tarray ["<<i<<"]"<<"= "<<array[i]<<endl;
  31.         }
  32.     return 0;
  33. }


Sample Input  output on C compiler
cpp tutorial to reverse an integer array using function and for loop



This tutorial is helpful to understand the array indexing and how to swap the index values. Its is recommended for beginners to edit and do experiment with code. Best of luck for learning cpp programming language.
 Find More Basic Examples here
C++ simple examples
Read More...

Passing array to a function cpp code

Leave a Comment
 This Cpp Tutorial contains
  • 3 ways to pass an array to a function
  • Each way and its usage
  • 3 c++ codes examples
  • How codes are working 
  • Sample output
  • Compiler used "C++ CodeBlocks Compiler"

Write a program which declares an array, initialize it then pass to a function. Function takes square of array values and update it. Show updated values in main function.

Concept used:
How programs works
  •   Declare array of size 10
  •   Initialize it using for loop
  •   Calling "UpdateArray" function and passing array as           argument
  •   Take squares of all values and update it
  •   Display final result in main function
 
Note: Function return type is void why? 
          As we know array name is constant pointer memory location if we get this location we can update it after updating if we access this memory location any where in the program we will get updated values. So no necessarily needs to return array or pointer.

First way
As we know array name is a constant pointer if we can get the this pointer we can access all values of array.
declaring  array in function parameter
Prototype: void  UpdateArray(int array[] );

 c++ code

  1.  #include <iostream>
  2.  using namespace std;
  3.  void UpdateArray(int array[])
  4.  {
  5.      for(int i=0;i<=9;i++)
  6.           array[i]=array[i]*array[i];
  7.  }
  8.  int main()
  9.  {
  10.      int array[10];
  11.      for(int i=0;i<=9;i++)
  12.           array[i]=i+1;
  13.      UpdateArray(array);
  14.  // Updated Values of Array
  15.      for(int i=0;i<=9;i++)
  16.           cout<<array[i]<<endl;
  17.      return 0;
  18.  }


Second way
Prototype: void  UpdateArray(int *array );
Declaring an integer pointer in function parameter
This pointer will receive arrays name(constant pointer) and we can access all values and update too.

  1.   #include <iostream>
  2.  using namespace std;
  3.  void UpdateArray(int *array)
  4.  {
  5.      for(int i=0;i<=9;i++)
  6.           array[i]=array[i]*array[i];
  7.  }
  8.  int main()
  9.  {
  10.      int array[10];
  11.      for(int i=0;i<=9;i++)
  12.           array[i]=i+1;
  13.      UpdateArray(array);
  14.  // Updated Values of Array
  15.      for(int i=0;i<=9;i++)
  16.           cout<<array[i]<<endl;
  17.      return 0;
  18. }


Third way
 In this way we pass two arguments one is arrays name and second   is its size
 In function receiving parameters we declares an array or integer pointer (your choice) to receive address of array and another integer variable size which contains the size of array.
Prototype:  void  UpdateArray(int *array, int size );

  1.  #include <iostream>
  2.  using namespace std;
  3.  void UpdateArray(int *array, int size)
  4.  {
  5.      for(int i=0;i<size;i++)
  6.           array[i]=array[i]*array[i];
  7.  }
  8.  int main()
  9.  {
  10.      int array[10];
  11.      for(int i=0;i<=9;i++)
  12.           array[i]=i+1;
  13.      UpdateArray(array,10);
  14.  // Updated Values of Array
  15.      for(int i=0;i<=9;i++)
  16.           cout<<array[i]<<endl;
  17.      return 0;
  18.  }
 Sample output all the codes have same
passing array to a function cpp code different ways
more Arrays Examples Here
C++ Simple Examples
For best practice do experiment with codes and examine output

if any confusion found dry run the code on a paper.
Read More...