GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-May-2005, 11:14
glulu76 glulu76 is offline
New Member
 
Join Date: Apr 2005
Posts: 22
glulu76 is on a distinguished road

Help with an array using pointers


I have to write a program that defines an array of ten pointers to floating point numbers. Then read from the keyboard ten numbers into the individual loactions accessed by the pointers. Then add all of the numbers and store the result in a pointer accessed location. Diplay the contents of all of the locations including the total. Then finally display the array in reverse using pointers and pointer arthimetic.

I think I have created a program that does all of this but I had to do it using integer. I can' t get it to work using float.

Can somebody please help me.

CPP / C++ / C Code:
#include <iostream.h>
#include<stdlib.h>
int main ()
{
int i, n = 10, total = 0; // declare array size

int *num =   new int [n];// create the array.

for(i = 0; i <n; i++)
{
	cout<< "Enter a number: ";//to allow the user to input the numbers into the array.
	cin>>  num[i];
}

cout<< " The values stored in the array are  " ;
for (i = 0; i < n; i++)
{
cout << "\n  " << num[i]; 
}
cout<< endl;

for (i =0; i < n; i++)
total = total + *num++;
cout << "The total of the array numbers is " << total<< endl;//To display the total of all numbers in the array.
cout<<" The array printed back words is  "<<endl;
for (i=9;i>=0;i--)// to go throught the array staring at highest index to the first index.
cout << *(--num) << " ";//decrement the pointer before using it.

return 0;
}

  #2  
Old 01-May-2005, 11:21
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Well, if it works for integer, than why wouldn't it work for floats?
Just create a pointer to float array, instead of pointer to int. The behaviour should be the same:

CPP / C++ / C Code:
float *num = new float [n];// create the array.

Regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
array of pointers to ...... kai85 C++ Forum 1 18-Apr-2005 17:06
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26
array of pointers to strings mirizar C++ Forum 5 21-Jan-2005 10:24

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:10.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.