6 Replies - 784 Views - Last Post: 02 April 2013 - 11:47 PM Rate Topic: -----

#1 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Comparing array to an int

Posted 02 April 2013 - 07:49 PM

I am at a point in my program I am writing for my assignment where I have an integer (which is an average value) and I want to compare the contents of an array with the integer. I have an array that holds household income values and I need to display which of those household income values EXCEED the average value.

I tried writing it like this:
for (c = 0; c < SIZE; c++)
{
if (incomeptr[c] > avgincome)
{
cout << incomeptr[c];
}
}



I think I understand why it is not giving me the output I want, but I have been fiddling with this part of my program for a while and can't think of any other way to write it that makes sense or that works.

Any advice is very much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Comparing array to an int

#2 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • Joined: 22-May 12

Re: Comparing array to an int

Posted 02 April 2013 - 07:56 PM

what is it doing/ outputting? are there any errors?

This post has been edited by Midi_: 02 April 2013 - 07:56 PM

Was This Post Helpful? 0
  • +
  • -

#3 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: Comparing array to an int

Posted 02 April 2013 - 07:59 PM

I am trying to compare the numbers I have in the array to an average value. If there is a number, or multiple numbers, in the array that are greater than the average value then display those numbers.

There are no errors notified by my compiler, but I am not getting the output I want. I am getting the actual coded value of incomeptr[c] which is a messy combination of numbers and letters. I need a list of the greater-than-average values in the array.

This post has been edited by 4N63L-Z3R0: 02 April 2013 - 08:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: Comparing array to an int

Posted 02 April 2013 - 09:51 PM

What type of variables are incomeptr and avgincome?

Jim
Was This Post Helpful? 0
  • +
  • -

#5 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: Comparing array to an int

Posted 02 April 2013 - 10:05 PM

incomeptr is an int pointer and avgincome is an int.

Here is my program so far so you can see where I am at:
// Midterm_Survey_N.cpp : Defines the entry point for the console application.

// This program belongs to NM.
#include "stdafx.h"
#include <iostream>
using namespace std;

// Function prototypes.
void incomeAvg (int *, int *, int);
void poverty (int *, int *, int);
// Function 3

int main()
{
	const int SIZE = 13;
	int id[SIZE];			// household id number - keep at 4 digits
	int income[SIZE];		// household annual income
	int memberNum[SIZE];	// household member number
	int c;

	// ***Assigned pointers*** ******************* //
	int *idptr;
	idptr = id;
	
	int *incomeptr;
	incomeptr = income;

	int *memberptr;
	memberptr = memberNum;
	// *********************** ****************** //

	cout << "Enter 4-digit household ID number.\n";
	for (c = 0; c < SIZE; c++)
	{
		cout << "Household " << (c+1) << ": ";
		cin >> id[c];
	}

	cout << "\nEnter household's annual income.\n";
	for (c = 0; c < SIZE; c++)
	{
		cout << "Household " << (c+1) << ": $";
		cin >> income[c];
	}

	cout << "\nEnter number of members in household.\n";
	for (c = 0; c < SIZE; c++)
	{
		cout << "Household " << (c+1) << ": ";
		cin >> memberNum[c];
	}

	// Function calls.
	incomeAvg (incomeptr, idptr, SIZE);
	poverty (incomeptr, memberptr, SIZE);
	// Function call 3

	return 0;
}

// Function headers.
void incomeAvg (int *incomeptr, int *idptr, int SIZE)
{
	int totalincome = 0, avgincome = 0;
	int c;

	// Total income (for average).
	for (c = 0; c < SIZE; c++)
	{
		totalincome += incomeptr[c];
	}

	// Average income.
	for (c = 0; c < SIZE; c++)
	{
		avgincome = totalincome / SIZE;
	}

	// Display the average income.
	cout << "The average income between all households is: $" << avgincome << "." << endl;

	// Display the EXCEEDING incomes.
	cout << "-------Households that exceed the average income-------" << endl;
	cout << "    Household ID        Exceeding Income";
	////////////////////////
	// More to come here. // This is where I am having the trouble.
	////////////////////////

	cout << endl;
}

void poverty (int *incomeptr, int *memberptr, int SIZE)
{
	double pov, percentage;
	int c;

	// Formula to calculate poverty line.
	for (c = 0; c < SIZE; c++)
	{
		pov = 6500.00 + 750.00 * memberptr[c];
	}

	// Calculates percentage of households UNDER poverty line.
	////////////////////////
	// More to come here. //
	////////////////////////
}

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: Comparing array to an int

Posted 02 April 2013 - 10:20 PM

Where are trying to compare any numbers?

Jim
Was This Post Helpful? 0
  • +
  • -

#7 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • Joined: 22-May 12

Re: Comparing array to an int

Posted 02 April 2013 - 11:47 PM

^^^ yeah you have a bunch of loops and functions, but no method of comparison. meaning the second code that you gave us doesn't include the first code you showed. Also you dont have the names/place holders of the parameters you are passing when you declare your function at the top.

here
void poverty (int *, int *, int);
void poverty (int *incomeptr, int *memberptr, int SIZE)


even if your compiler lets you getaway with it, its still good form to do it.

View Postjimblumberg, on 02 April 2013 - 10:20 PM, said:

Where are trying to compare any numbers?

Jim


oh I see it, I think he means to put that code at the top in around line 86

also you dont need avg income in a loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1