Arrays

Natalyias77 (10)
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;
}
Moschops (5956)
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
}
Natalyias77 (10)
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
Marcos Modenesi (26)
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.
Natalyias77 (10)
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;

}
LowestOne (767)
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); 
}
Registered users can post here. Sign in or register to post.