C++ homework - wrong result

We've got a string of n numbers. Each number should be replaced with its closest integer and the string must be shown from the last to the first.
Example: for n=6 and 2.72, 4.34, 9.82, 1.0, 4.05, 2.45 the program will output 2 4 1 10 4 3
This is how I've tried to do it, but after I enter the first value it displays a negative number. Can't really figure out why.

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
  // 1212.cpp : Defines the entry point for the console application.
//

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

using namespace std;


int main()
{
	int v[20], n, aux;
	cin >> n;
	for (i = 1; i <= n; i++)
	{
		cin >> v[i];
		for (i = 1; i <= n / 2; i++)
		{
			aux = v[i];
			v[i] = v[n - i];
			v[n - i] = aux;
		}
		for (i = 1; i <= n; i++)
		{
			if ((v[i] - (int)v[i]) >= 1 / 2)
			{
				v[i] = (int)v[i] + 1;
			}
			else {
				v[i] = (int)v[i];
			}
		}
	}
	cout << v[i];
}
you don't have i declared in for loop, it should be like " for(int i = 1; i <= n; i++) " and the other loops should have different declared counters in them like "for (int j = 1; j <= n / 2; j++) and "for (int m = 1; m <= n; m++)" and accordingly you will have to change the indexes in loop for the array. I haven't looked through for logical errors, but I noticed these technical ones, so I don't guarantee you, that correcting them will solve your exercise :)
Hello forta2k,

Sorry to say most of your program is wrong and does not work.

In main line 12 you need to initialize your variables especially the array. A set of empty {}s after the variable is the simplest way. Otherwise you are using whatever is at the memory location set aside by the compiler and used when the program runs. Generally referred to as garbage.

ALWAYS best to initialize your variables.

You have defined an array if ints and then try to enter a double into this array. What happens is that the portion to the right of the decimal point will be dropped and only the whole number is stored.

The for loops are a problem:

First arrays are zero based and your for loops all start at 1. Which means you loose the first element, element zero, of the array, so you are actually using only 19 elements of the array not the 20 that is available.

The next two for loops are not working the way you may have intended because they are working with some garbage values because the array has not been filles yet and the array was not initialized.

As I understand your instructions and by what you have done so far:


1. Prompt for how many numbers will be used.
2. Check that this number does not exceed 20.
3. Enter all the numbers into the array. Prompt for each number.
4. Access the array in reverse order for output.
5. Determine if the the number should be rounded up or down.
6. Print result.



A general idea of what you need to do. I will work on the program and see what I can come up with.

Hope that helps.

Andy
@nick2361,

Mostly correct.

Defining "i" outside the for loop is OK. This just allows you to have access to the value if "i" when the for loop ends and i" would loose scope. It is handy once in awhile.

Andy
@Handy Andy, I can't see "i" defined anywhere :D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cmath>

int main()
{
    const double numbers[] = { 2.72, -2.72, 4.34, 9.82, 1.0, -1.0, 4.05, 2.50, -2.50, 2.49, -2.49 } ;
    const int N = sizeof(numbers) / sizeof( numbers[0] ) ;

    // print rounded integer values in reverse order
    // assumes that the rounded values are within the range of long long
    // http://en.cppreference.com/w/cpp/numeric/math/round
    for( int i = N-1 ; i >= 0 ; --i ) std::cout << std::showpos << std::llround( numbers[i] ) << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/3dd47574064fd505
@nick2361,

I see your point now. The "i" I was thinking of was int he code I corrected. Sometimes I work on several different programs at one time that I loose track of where I am at.

Andy

Edit:
Last edited on
Topic archived. No new replies allowed.