Arrays

I am writing a program for school that is supposed to take an array and print its contents.
I have reached out to the teacher but I don't know when I will hear back.

I keep getting errors:

Errors:
1>------ Build started: Project: Assignment 9 - Array Homework, Configuration: Debug Win32 ------
1> Assignment 9 - Array Homework.cpp
1>Assignment 9 - Array Homework.obj : error LNK2028: unresolved token (0A00031F) "void __cdecl print_integers(int * const,int)" (?print_integers@@$$FYAXQAHH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>Assignment 9 - Array Homework.obj : error LNK2019: unresolved external symbol "void __cdecl print_integers(int * const,int)" (?print_integers@@$$FYAXQAHH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>C:\Users\sxc879\documents\visual studio 2010\Projects\Assignment 9 - Array Homework\Debug\Assignment 9 - Array Homework.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Here is my program:

// Assignment 9 - Array Homework.cpp : main project file.

#include "stdafx.h"
#include <iostream>

using namespace std;

const int ARRAY_SIZE = 5;

//int round (float x); // rounds any float to the nearest int

void print_integers(int aRay[], int size); //prints out the rounded value of every element of the array


int main()
{

int listA[ARRAY_SIZE] = {0};

cout << "List A elements are: ";
print_integers(listA, ARRAY_SIZE);
cout << endl;

system ("pause");

return 0;
}
You have to write the function print_integers.

Here's something to get you started:

1
2
3
4
print_integers(int aRay[], int size)
{
   // you write code here to print the array
}
Thank you. This is my first time with arrays and functions. The book I am using was not clear on calling an array as a function.

So I have updated my code but I can't get the function to print. It will call the function, but not print out my array.

I just want to work on this piece then I will work on rounding.

Thoughts?

Plus how do I comment out my code?


void print_integers(int aRay[], int size)
{
int index;

for(index = 0; index < size; index++)
cout << "Rounded value of every element: " << aRay[index] << " " ;
}
Last edited on
What kind of truoble do you get? compiler messages?

1
2
3
4
5
6
7
void print_integers(int aRay[], int size)
{
int index;

for(index = 0; index < size; index++){
cout << "Rounded value of every element: " << aRay[index] << " " ;}
}


I think you were missing a couple of {}, but I´m not sure.
To comment a code, use //comment comment comment...(to the end of line)

or /*comment comment comment*/

Try posting your code using the code format button, at the right of the text input window.
So after working on this all day this is what I came up with.
I have gotten closer to the end result but not quite yet there.

What I now need to do is to call function round . The code for this function is supposed to take the array of float (I know I have it all as int right now) and round it off, returning the value to the nearest integer. No C++ libraries.

But so far this is what I have. It runs as I need it to. Any ideas on how to code to round off without C++ libraries?

Thanks for the hint on posting code :-)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
int round (int x); // rounds any float to the nearest int
void print_integers(int aRay[], int size); //prints out the rounded value of every element of the array


int main()
{
	int sales[10];
	int index;

	print_integers(sales, index);

	system ("pause");

return 0;
}

void print_integers(int aRay[], int size)
{

	//Reads data into the array
	int list[10];
	int bulk;
	for (bulk = 0; bulk < 10; bulk++) 
	{	
	cout << "Enter a number:";
	cin >> list[bulk];
	cout << endl;
	}

	// Call to function round
	int z;
	round(z);


	// prints out what number was entered	
	int i;
	for (i = 0; i < 10; i++)
	{
	cout << "Number entered: " << list[i];
	cout << endl;
	}


}

int round (int x)
{
	cout << "Calling Round!!" << endl;

	//add code to round an element

	return x;

}
Add an amount to the float that will make the float be the right value when it is trunkated:
1
2
3
4
int floatToInt(float in)
{
  return (int)(in + 0.5f); 
}
I am able to input numbers from the keyboard and have them print.

If the statement is a float, then I can enter all my numbers as decimals, but they print out the same: float list[5];

If the statement is an int, then I can only enter int. If I enter a float it throws up googly gook. int list[5];

It needs to be able to input as float, output rounded value as int.

Suggestions?

Complete program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
int round (float x); // rounds any float to the nearest int
void print_integers(int aRay[], int size); //prints out the rounded value of every element of the array

int main()
{
	int bRay[5];
	int bSize;

	print_integers(bRay, bSize);

	system ("pause");

return 0;
}

void print_integers(int aRay[], int size)
{

	//Reads data into the array
	int list[5];
	int bulk;
	for (bulk = 0; bulk < 5; bulk++) 
	{	
	cout << "Enter a number:";
	cin >> list[bulk];
	cout << endl;
	}

	// Call to function round
	round(list[bulk]);


	// prints out what number was entered	
	int i;
	for (i = 0; i < 5; i++)
	{
	cout << "Number entered: " << list[i];
	cout << endl;
	}


}

int round (float x)
{
	cout << "Calling Round!!" << endl;
	
	return (int)(x + 0.5f);

}
closed account (3qX21hU5)
First off a little tip for your for loops. You are currently writing them like this
1
2
int i;
for (i = 0; i < 5; i++)


But you can write them like this


for (int i = 0; i < 5; i++)

Both examples work almost the same way. Notice that I'm declaring my index int i; inside the for loop. Now the only difference is that when you declare the variable inside the for loop it disappears when the loop is done, so you will no longer be able to use that variable after that.

Like I said both work almost the same, but it is more common to put it inside the for loop. Anyways now on to the problem.


Somethings I noticed

1) you declare bSize but you don't initialize it to anything. You are also passing it to your function but you don't use it in your function anywhere. When I say you don't use it I mean you don't use the int size parameter in your function.

2) The reason why you are getting a random output when you enter decimal numbers is because you are trying to enter a decimal into a integer array. To fix this you need to change your array to a double type like double bRay[5], and change your function to accept a double as a parameter, and everything else that deals with the array.

That should fix up the problems so far and leave you to working on rounding up to a whole number part.
Last edited on
Topic archived. No new replies allowed.