Here's a simple program I wrote.

/* Use a single-subscripted array to solve the following problem. 

A company pays its salespeople on a comission basis. The salespeople receive $200 per week plus
9 percent of their gross sales for that week. For example, a salesperson who grosses $3000 in 
sales in a week receives $200 plus 9 percent of $3000, or a total of $470. 

Write a C program (using an array of counters) that determines how many of the salespeople
earned salaries in each of the following ranges (assume that each salesperson's salary is
truncated to an integer amount):
 a) $200-299
 b) $300-399
 c) $400-499 
 d) $500-599 
 e) $600-699 
 f) $700-799 
 g) $800-899
 h) $900-999
 i) $1000 and over. */ 

// Create an array
// Get user input on the weekly gross per person and store it into an array
// Extract each gross. Take 9% of the gross and add 200 to get the salary per person.
// Store the gross in an array.
// Search the array for salaries lieing in the ranges above.
// Print the results.

#include <stdio.h>

#define SIZE 5

int main(void)
{
	int gross[SIZE];
	int cnt = 0, i = SIZE, j = 0;
	int s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0, s7 = 0, s8 = 0, s9 = 0, s10 = 0;

	puts("Enter up to 5 weekly gross values");
	for (cnt = 0; cnt < SIZE; cnt++)
	{
		if (scanf("%d", &gross[cnt]) == 1)
		{
			gross[i] = ((gross[cnt] * .09) + 200);
			i++;
		}
	}

	for (j = SIZE; j < i; j++)
	{
		if (gross[j] >= 200 && gross[j] <= 299)
			s2++;
		if (gross[j] >= 300 && gross[j] <= 399)
			s3++;
		if (gross[j] >= 400 && gross[j] <= 499)
			s4++;
		if (gross[j] >= 500 && gross[j] <= 599)
			s5++;
		if (gross[j] >= 600 && gross[j] <= 699)
			s6++;
		if (gross[j] >= 700 && gross[j] <= 799)
			s7++;
		if (gross[j] >= 800 && gross[j] <= 899)
			s8++;
		if (gross[j] >= 900 && gross[j] <= 999)
			s9++;
		if (gross[j] >= 1000)
			s10++;
	}

	printf("There were %d employess that earned between $200-$299\n", s2);
	printf("There were %d employess that earned between $300-$399\n", s3);
	printf("There were %d employess that earned between $400-$499\n", s4);
	printf("There were %d employess that earned between $500-$599\n", s5);
	printf("There were %d employess that earned between $600-$699\n", s6);
	printf("There were %d employess that earned between $700-$799\n", s7);
	printf("There were %d employess that earned between $800-$899\n", s8);
	printf("There were %d employess that earned between $900-$999\n", s9);
	printf("There were %d employess that earned over $1000\n", s10);

	return 0;
}

I used the unused portion of the array gross to store the salaries. When I execute this program it crashes and I can't figure out why =\.

Recommended Answers

All 2 Replies

Your loop is trying to access outside the range of the array.

Your array is size 5, that means, the elements are indexed by the numbers 0-4... yet your for loop says:

for (j = SIZE; j < i; j++)

You need to loop from j=0 to j<SIZE


also, you might want to check this..

for (cnt = 0; cnt < SIZE; cnt++)
{
	if (scanf("%d", &gross[cnt]) == 1)
	{
		gross[i] = ((gross[cnt] * .09) + 200);
		i++;
	}
}

Remember that you've initialised i with the value of SIZE... you have the same problem here as above.

(I don't really see why you use the variable 'i' anywhere in your program - you don't actually need it..)

oh ok i fixed it. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.