C++ Programming Basics tutorials, C++ Programs Examples, Variables, Operators, Comments and Data Types in C++, Keywords in C++, C++ Expressions, Control Structures, Decision Making Structures, Loops(for loop, while loop, Do-while-Loop) in C++ all in cpp programming tutorials.
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
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
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char key_press;
int ascii_value;
cout<<"\n\t\t\tPress Any Key To Check Its ASCI Value\n\n\t\t\tPress ESC to EXIT\n\n\n";
while(1)
{
key_press=getch();
ascii_value=key_press;
if(ascii_value==27) // For ESC
break;
cout<<"\t\t\tKEY Pressed-> \" "<<key_press<<" \" Ascii Value = "<<ascii_value<<"\n\n";
}
return 0;
}
Sample input outputs
Its a simple program which helps the beginner to control input keys.
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 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.
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:
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
#include <iostream>
using namespace std;
void UpdateArray(int array[])
{
for(int i=0;i<=9;i++)
array[i]=array[i]*array[i];
}
int main()
{
int array[10];
for(int i=0;i<=9;i++)
array[i]=i+1;
UpdateArray(array);
// Updated Values of Array
for(int i=0;i<=9;i++)
cout<<array[i]<<endl;
return 0;
}
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.
#include <iostream>
using namespace std;
void UpdateArray(int *array)
{
for(int i=0;i<=9;i++)
array[i]=array[i]*array[i];
}
int main()
{
int array[10];
for(int i=0;i<=9;i++)
array[i]=i+1;
UpdateArray(array);
// Updated Values of Array
for(int i=0;i<=9;i++)
cout<<array[i]<<endl;
return 0;
}
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 );
#include <iostream>
using namespace std;
void UpdateArray(int *array, int size)
{
for(int i=0;i<size;i++)
array[i]=array[i]*array[i];
}
int main()
{
int array[10];
for(int i=0;i<=9;i++)
array[i]=i+1;
UpdateArray(array,10);
// Updated Values of Array
for(int i=0;i<=9;i++)
cout<<array[i]<<endl;
return 0;
}
Sample output all the codes have same
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.