This manual focuses on the concepts like functions, pointers and arrays. As a beginner of C++ programmer, your task is to read these concepts given below and then write the programs given at the end of this manual. You can find similar articles on http://www.mycplus.com

Pointers

A pointer is a variable that designates the address, or location in memory, of some other particular type variable.

Declaration

As a variable is declared before it is used, similarly, a pointer also has to be declared before it is used. For example:

  • int myVar;  (An integer type variable)
  • int *myPtr; (A pointer that points to an integer variable)

It is read from left to write as; myPtr is a pointer that points to an integer type variable.

 Assignment

Before performing any operation on the declared pointer, the pointer must be initialized with address of a particular type variable. For example:

  • myPtr = &myVar;

The above statement means that the integer variable named myVar is pointed by an integer pointer named myPtr.

 Value retrieval (dereferencing)

For accessing the value stored in the variable to which the pointer is pointing to. For example:

  • cout<<*myPtr;

The above statement will print the same value as the following statement will

  • cout<<myVar;

The programmer can even change the value of the variable by using the pointer. For example:

  • *myPtr++;
  • cout<<*myPtr;  // The printed value will be one greater than previous value

The interaction of pointers and arrays can be confusing but here are two fundamental statements about it:

  • A variable declared as an array of some type acts as a pointer to that type when used by itself. It points to the first element of the array.
  • A pointer can be indexed like an array name.

Example

#include  // include the iostream, the header file
using namespace std; // syntax for using the namespace std from
    // the header file, iostream

	void function1(int *); // declaring the ptototype

	int main()
	{
		int   intArray[] = {12, 31, 43, 56, 42 };
		int   *myPtr;   // An integer type pointer
		myPtr = intArray; // BECAUSE intArray = &intArray[0]

		function1(myPtr); // Function called

		getchar(); // press a key to exit
		return 0;
	}

	void function1(int *ptr)
	{
		cout< ";
		cin>>array_store[i];
	}
	cout<	return 0;
}
int sum(double *arr,int size)
{
	int sum=0;
	for(int i=0;i	{
		if(i%2==0)
		{

			sum+=pow(*(arr+i),3);
		}
		else
		{
			sum+=pow(*(arr+i),2);
		}
	}
	return sum;
}

Exercise 

1- Make a program which generates the series up to user defined number of terms. The general formula for the series is

2n*xn-1

Your program should consist of atleast two functions. One function should display the sum while other function should calculate and return the sum of series upto user defined limit

#include<iostream>
#include<cmath>
void series_display(int);
int sum_series(int);
using namespace std;
int main()
{
	int n;
	cout<<"Enter Number of terms in series : ";
	cin>>n;
	series_display(n);
	cout<<sum_series(n)<<endl;
	return 0;
}
void series_display(int x)
{
	int i,ser;
	double dummy;
	for(i=0;i<x;i++)
	{
		dummy=i;
		ser=2*i*pow(2,dummy-1);
		cout<<ser<<" ";
	}
	cout<<endl;
}
int sum_series(int y)
{
	int i,sum=0,temp;
	double dummy;
	for(i=0;i<y;i++)
	{
		dummy=i;
		temp=2*i*pow(2,dummy-1);
		sum+=temp;
	}
	return sum;
}

2- Write a program to take ten numbers as input from the user, store it in an array and then find the sum of cube (using another function) of all the elements stored at even indexes and sum of squares of all the elements stored at odd indexes of the array. The sum should be calculated inside a function.
Sum = (arr[0]3+arr[1]2+arr[2]3+………)

#include<iostream>
#include<cmath>
int sum(double * ,int );
using namespace std;
int main()
{
	double array_store[10];
	for(int i=0;i<10;i++)
	{
		cout<<"Enter element at array["<<i<<"] > ";
		cin>>array_store[i];
	}
	cout<<sum(array_store,10);
	return 0;
}
int sum(double *arr,int size)
{
	int sum=0;
	for(int i=0;i<size;i++)
	{
		if(i%2==0)
		{

			sum+=pow(*(arr+i),3);
		}
		else
		{
			sum+=pow(*(arr+i),2);
		}
	}
	return sum;
}
Tagged with: C/C++ languageProgrammingSource Code
 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.