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 09-Nov-2004, 01:45
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road

Help on simple arrays


Hello again, I have encountered another problem, this time, dealing with arrays. I need help in correcting my program that I have posted below to meet the requirements of the assignment.

"Write a program that lets the user enter 10 values into an array. The program should then display all values as in the example format shown below:"

You have entered:

3

4

8

3

5

9

0

45

4

3

Here is my program so far:
CPP / C++ / C Code:
#include <iostream>
#include <cctype>
#include <iomanip>

using namespace std;

#define MAX 11  //Define MAX

int main()
{

float numbers[MAX];
int count = 0;
int loop = 0;

cout << "Enter ten numbers, separated by commas:" << endl;


for(loop=1; loop < MAX; loop++)//for loop
{
	cin >> numbers[loop];
}

	cout << numbers[loop];
	cout << "You have entered:" << numbers << endl << endl;
    
	return 0;
}

I want to modify it to 1) allow the user to enter 10 numbers, separated by commas (which I haven't learned how to do), and 2) output all the numbers as in the example format above. Any help you can offer will be truly appreciated. Thanks again!
  #2  
Old 09-Nov-2004, 19:31
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by brookeville
Hello again, I have encountered another problem, this time, dealing with arrays. I need help in correcting my program that I have posted below to meet the requirements of the assignment.

"Write a program that lets the user enter 10 values into an array. The program should then display all values as in the example format shown below:"

You have entered:

3

4

8

3

5

9

0

45

4

3

Here is my program so far:
CPP / C++ / C Code:
#include <iostream>
#include <cctype>
#include <iomanip>

using namespace std;

#define MAX 11  //Define MAX

int main()
{

float numbers[MAX];
int count = 0;
int loop = 0;

cout << "Enter ten numbers, separated by commas:" << endl;


for(loop=1; loop < MAX; loop++)//for loop
{
	cin >> numbers[loop];
}

	cout << numbers[loop];
	cout << "You have entered:" << numbers << endl << endl;
    
	return 0;
}

I want to modify it to 1) allow the user to enter 10 numbers, separated by commas (which I haven't learned how to do), and 2) output all the numbers as in the example format above. Any help you can offer will be truly appreciated. Thanks again!
Put the output in another for loop. That's about all you need.
__________________

Age is unimportant -- except in cheese
  #3  
Old 10-Nov-2004, 02:16
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road

New program


Walt, how about this? I fixed a few errors but still would like to know how to input numbers separated by commas, like 1,2,3,4,5,6,7,8,9,0, etc... in my array.
CPP / C++ / C Code:
#include <iostream>
#include <cctype>
#include <iomanip>

using namespace std;

#define MAX 11  //Define MAX

int main()
{

float numbers[MAX];
int count = 0;
int loop = 0;

cout << "Enter ten numbers: " << endl;


for(loop=1; loop < MAX; loop++)//for loop
{
	cin >> numbers[loop];
}

cout << "\n\nYou have entered:\n\n";

for (loop=1; loop < MAX; loop++)
	{
		cout << numbers[loop] << endl << endl;
	}

return 0;	
}

Also, my teacher suggested that I try another program for fun. It was one right from the book that deals with arrays. It asks "Write a program that lets the user enter 10 values into an array. The program should then display those values in ascending order." I'll post it when I (hopefully) get it done!
  #4  
Old 10-Nov-2004, 12:19
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by brookeville
Walt, how about this? I fixed a few errors but still would like to know how to input numbers separated by commas, like 1,2,3,4,5,6,7,8,9,0, etc... in my array.
CPP / C++ / C Code:
#include <iostream>
#include <cctype>
#include <iomanip>

using namespace std;

#define MAX 11  //Define MAX

int main()
{

float numbers[MAX];
int count = 0;
int loop = 0;

cout << "Enter ten numbers: " << endl;


for(loop=1; loop < MAX; loop++)//for loop
{
	cin >> numbers[loop];
}

cout << "\n\nYou have entered:\n\n";

for (loop=1; loop < MAX; loop++)
	{
		cout << numbers[loop] << endl << endl;
	}

return 0;	
}

Also, my teacher suggested that I try another program for fun. It was one right from the book that deals with arrays. It asks "Write a program that lets the user enter 10 values into an array. The program should then display those values in ascending order." I'll post it when I (hopefully) get it done!
Doing this will require
  • the horrendous scanf()
  • higher level function that I don't like using (strtok())
  • parsing the input line yourself

I'd recommend for now not worrying about it. You'll get into that kind of stuff soon enough.
__________________

Age is unimportant -- except in cheese
  #5  
Old 10-Nov-2004, 17:34
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road
Quote:
Originally Posted by WaltP
Doing this will require
  • the horrendous scanf()
  • higher level function that I don't like using (strtok())
  • parsing the input line yourself

I'd recommend for now not worrying about it. You'll get into that kind of stuff soon enough.


I see... Well, as long as my program meets the requirements of the assignment, it should be perfectly fine. I'll take your advice and wait until I learn about it, before attempting it. Thanks again, Walt!
 
 

Recent GIDBlogHalfway done! 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
Knight tour (arrays help needed) dilmv C++ Forum 7 18-Oct-2004 14:31
problem with arrays melas C Programming Language 5 12-Oct-2004 04:18
need help with passing 3 arrays into a function tommy69 C Programming Language 14 07-Apr-2004 00:22
Problem multiplying arrays hellhammer C Programming Language 9 29-Mar-2004 15:32

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

All times are GMT -6. The time now is 17:14.


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