Jump to content
xisto Community
Sign in to follow this  
iGuest

Basic C++ Language Getting started with C++

Recommended Posts

Next thing we would want to do is talk about arrays

An array is an ordered list of values.

#include<stdio.h>int main(int argc, char *argv[]){int arr[10];int j;for(int j=0;j<10;j++){arr[j] = j * 3;}for(int j=0;j<10;j++){printf("the %d th value of arr is \n",j,arr[j]);}return 0;}

the above example demonstrates initializing an integer array of size 10 with multiples of 3 in the first loop.

the next loop prints these vales in that order in which they were stored.

Next example demonstrates a two dimensional array.


#include<stdio.h>int main(int argc, char *argv[]){int arr[10][10];int i,j;for(i=0;i<10;i++){ for(int j=0;j<10;j++)  {  arr[i][j] = i * j ;  }}for(i=0;i<10;i++){ for(int j=0;j<10;j++)  {  printf("the arr[%d][%d] has value :%d \n",i,j,arr[i][j]);  }}return 0;}

Share this post


Link to post
Share on other sites

Next we can try to understand a basic philosophy that everything is done in functions.lets see how we can use some of the previous examples to understand basics of functions.

#include<stdio.h>void printArr(int arr[]);int main(int argc, char *argv[]){int array[10];int j;for(int j=0;j<10;j++){array[j] = j * 3;}printArr(array);return 0;}void printArr(int arr[]){int j;for(j=0;j<10;j++){printf("the %d th value of arr is \n",j,arr[j]);}}

the reult of this program is sam, it assigns and then prints the values of array;but the function of printing values is carried out by a separate function called printArr which does not return any value but carries out the task of printing the values.

Share this post


Link to post
Share on other sites
#include<stdio.h>void printArr(int arr[]);int getMultiple(int a, int ;int main(int argc, char *argv[]){int array[10];int j;for(int j=0;j<10;j++){array[j] = getMultiple(j, 3 ) ;}printArr(array);return 0;}void printArr(int arr[]){int j;for(j=0;j<10;j++){printf("the %d th value of arr is \n",j,arr[j]);}int getMultiple(int a, int {return a * b;}}

in this example the vales are assigned by calling the function getMultiple.this function returns the product of the arguments passed to it.

Edited by Umar Shah (see edit history)

Share this post


Link to post
Share on other sites

Very good tutorial, I already have experiance in both C and C++, and these tutorials certainly should do better than what I done, that being trial and error and looking at already programmed programs for assitance, I'm glad Umar Shah has decided to expand on this.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.