C++ program with arrays and functions

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 3256 Views - Last Post: 18 December 2010 - 05:05 PM Rate Topic: -----

#1 justshelme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-December 10

C++ program with arrays and functions

Posted 18 December 2010 - 01:58 PM

Hello everyone. I am going to be honest and say yes, this is homework. However, I am NOT looking for someone to give me a straight answer if I was there are a few posts already on this site that pretty much give me the answer to this exact problem. I am an online student and I just cannot learn programming and algorithm design on my own with some crummy book. So if you want to help me through this, please do; but if you want to reply with some rude comment about how I should be doing this on my own with no help, please don't. Like I'm not stressing enough without a strangers negativity being loaded on me. Thanks!

So I basically have to create a program that:
- allows users to enter 10 numbers
- store those number in an array
- data type to store numbers in array should be float or double
- create and use a function to calculate the average of the numbers in the array
- output the average

I keep getting error lnk 2019 and lnk 1120; both which are "unresolved externals"?

This is what I have so far:

#include <iostream>

using namespace std;

int main()
{
	double Sum; //declares sum as double float
	double Average; //declares average as double float
	double PositiveNumber[10]; //array of 10 positive numbers 
	const int i; // 
	Sum = 0;
	Average = 0;
	for (i = 0; i < 10; i++)
	{
		cout << "Please enter ten positive numbers : " << (i + 1) <<end1;
		cin >> PositiveNumber[i];
		if PositiveNumber[i] < 0
			{
				cout << "Please make sure your numbers are positive " << end1;
			}
		while PositiveNumber [i] > 0;
			{
				Sum = Sum + PositiveNumber[i]
			}
	}
	Average = Sum/10; // Computes average by dividing sum of positive numbers by 10
	cout << "The average of your numbers is " << Average << end1; // display the result
	return 0; // terminate with success
}


This post has been edited by no2pencil: 18 December 2010 - 02:21 PM
Reason for edit:: Added Code Tags


Is This A Good Question/Topic? 0
  • +

Replies To: C++ program with arrays and functions

#2 z3r0sh1ft  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 10-November 10

Re: C++ program with arrays and functions

Posted 18 December 2010 - 02:08 PM

First off, use the code tags for any code.

Secondly, on the line
const int i;

you are declaring a constant but aren't assigning any value to it.

Then in these lines
for (i = 0; i < 10; i++)

cout << "Please enter ten positive numbers : " << (i + 1) <<end1;

you are changing the value of i with i++ which was declared constant, not variable.
Also all the end1 are typed in with a 1 (one) not an l (letter).

This post has been edited by z3r0sh1ft: 18 December 2010 - 02:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 justshelme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-December 10

Re: C++ program with arrays and functions

Posted 18 December 2010 - 02:20 PM

Hi and thank you for replying!

I apologize but I do not know what you mean by code tags?

What I got from your post was that I should change int i to a non-constant. I had that a couple of hours ago but started reading about constants and got confused. Either way I still get the same error codes, the 2019 about "main referenced in function".
Was This Post Helpful? 0
  • +
  • -

#4 z3r0sh1ft  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 10-November 10

Re: C++ program with arrays and functions

Posted 18 December 2010 - 02:46 PM

Yes, i should not be constant if you intend to change its value.
Did you change all the end1 to endl (with an L not a 1)?

Your if and while conditions should go between parenthesis like so
if(PositiveNumber[i] < 0)


And you're forgetting the semicolon in line 23.
Your loop infinitely loops too, try to look into why that is. ;)
Was This Post Helpful? 1
  • +
  • -

#5 stefaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-November 08

Re: C++ program with arrays and functions

Posted 18 December 2010 - 03:17 PM

use one of these prototype for your function to calculate the average
/*calculate the average using a global array and output average from inside the function*/
void calculateAverage(); 

/*calculate the average by passing the array as an argument to the function and output average from inside the function*/
void calculateAverage(double myArray[]);

/*calculate the average using a global array and return the average (store in a double variable)*/
double calculateAverage();

/*calculate the average by passing the array as an argument to the function and return the average (store in a double variable)*/
double calculateAverage(double myArray[]);



This is an example using prototype 1
void calculateAverage()
{
	double sum = 0, average = 0;
	for (int i = 0; i < 10; i++)
	{
		sum = sum + myArray[i];
	}
	average = sum / 10; 
	cout << "Average is " << average;	

}


So this is what you should do using prototype 1

using namespace std;

//prototype 1
//declare array globally

int main()
{
	//populate array (get values from user)
	//call function ( calculateAverage() )
	return 0;
}



Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: C++ program with arrays and functions

Posted 18 December 2010 - 03:27 PM

Code tags:

:code:

Also, when you receive errors, copy and paste them EXACTLY as they appear in your development environment.
Was This Post Helpful? 0
  • +
  • -

#7 justshelme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-December 10

Re: C++ program with arrays and functions

Posted 18 December 2010 - 03:34 PM

#include <iostream>

using namespace std;

int main()
{
	double Sum; //declares sum as double float
	double Average; //declares average as double float
	double PositiveNumber[10]; //array of 10 positive numbers
	int i = 10
	Sum = 0; 
	Average = 0;
	for (i = 0; i < 10; i++)
	{
		std::cout << "Please enter ten positive numbers: " << (i + 1) << std::endl;
		std::cin >> PositiveNumber[i];
		if (PositiveNumber < 0.0);
		{
			cout << "Please make sure you have entered ten positive numbers: " << endl;
		}
		Sum = Sum + PositiveNumber[i];
	}
	Average = Sum/10; // Computes average by dividing sum of positive numbers by 10
	cout << "The average of your numbers is " << Average << endl; // display the result
	return 0; // terminate with success
}
	


thank you!

1>------ Build started: Project: FinalProgram, Configuration: Debug Win32 ------
1>Linking...
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\Justin\Documents\Visual Studio 2008\Projects\FinalProgram\Debug\FinalProgram.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Users\Justin\Documents\Visual Studio 2008\Projects\FinalProgram\FinalProgram\Debug\BuildLog.htm"
1>FinalProgram - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I do not completely understand prototype functions. Is it necessary to this working or am I just confusing it with the if statement?
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: C++ program with arrays and functions

Posted 18 December 2010 - 03:45 PM

The problem is you created a Win32 project rather than a Win32 CONSOLE project.

Go to Project->Project Properties->Configuration Properties->Linker->System and change the SubSystem dropdown value from Windows (/SUBSYSTEM: WINDOWS) to Console (/SUBSYSTEM: CONSOLE)

EDIT: Added to C++ FAQs topic under Common Windows/Visual Studio Errors
Was This Post Helpful? 0
  • +
  • -

#9 justshelme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-December 10

Re: C++ program with arrays and functions

Posted 18 December 2010 - 03:49 PM

It's already a console project.
Was This Post Helpful? 0
  • +
  • -

#10 Draps  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 103
  • Joined: 20-November 10

Re: C++ program with arrays and functions

Posted 18 December 2010 - 03:50 PM

View Postjustshelme, on 19 December 2010 - 07:34 AM, said:

I do not completely understand prototype functions. Is it necessary to this working or am I just confusing it with the if statement?


no its a layout style for a function, you did say that was one of the requirements of the task. to make a function in which you calculate the average.

stefaan posted one way to create a function to do what you want it too.
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: C++ program with arrays and functions

Posted 18 December 2010 - 03:56 PM

View Postjustshelme, on 18 December 2010 - 04:49 PM, said:

It's already a console project.


You are correct; sorry. I misread it as one of the most common errors we see here.

Can you post your current code and the errors you're receiving? With the code you last posted, I get OTHER errors...it doesn't even GET to the linking stage.
Was This Post Helpful? 0
  • +
  • -

#12 justshelme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-December 10

Re: C++ program with arrays and functions

Posted 18 December 2010 - 04:00 PM

View PostDraps, on 18 December 2010 - 02:50 PM, said:

View Postjustshelme, on 19 December 2010 - 07:34 AM, said:

I do not completely understand prototype functions. Is it necessary to this working or am I just confusing it with the if statement?


no its a layout style for a function, you did say that was one of the requirements of the task. to make a function in which you calculate the average.

stefaan posted one way to create a function to do what you want it too.


Sorry, you are correct! Great1 Another thing I know nothing about.

ok so when I create a function it will come before the int main() right? Will I need to take out some of what I already have in my main to put in the void function?
Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: C++ program with arrays and functions

Posted 18 December 2010 - 04:03 PM

Reading material for functions
Was This Post Helpful? 0
  • +
  • -

#14 justshelme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-December 10

Re: C++ program with arrays and functions

Posted 18 December 2010 - 04:37 PM

View PostJackOfAllTrades, on 18 December 2010 - 03:03 PM, said:



ok read that but dont understand how to apply it. Is any of this ever in laymens terms? If that is as simple as it gets I need to rethink my choices.

I dont understand how you know if it is a int function or a void function?
Was This Post Helpful? 0
  • +
  • -

#15 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: C++ program with arrays and functions

Posted 18 December 2010 - 04:58 PM

If programming were easy and its documents for laymen, then I wouldn't make the big bucks now, would I? ;)

You -- as the programmer creating the function -- decide what you want the function to do and consequently what it returns to the caller. If you're trying to get an integer from a function, then the function should return int. If you don't need the function to return anything -- for example, if the function just prints a value -- then the function returns void, or nothing. Then you get into returning values back through the variables passed to the function.

Your instructor surely covered this. Go back and read your notes and books. You seem to be looking for some spoon-feeding here.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2