We've started learning about arrays, but our lecture notes are really not helpful at all and we don't have a book. I'm struggling to understand this question and I honestly have no idea what to do or where to start. I've tried to read tutorials and things but I'm just not understanding it. If someone could help me step by step with this problem I would be really grateful.

The question:
Create an array of type float with 10 elements. Write a modular program that does the following:
• Read values into the array representing salaries.
• Give everyone a 20% pay rise!
• Display the new salaries.
• Find the largest salary and display this, along with the index where it occurs.

What I've got so far:

#include <stdio.h>

const int SIZE = 10;

/* Read user input */
int getData ()
{
int array[SIZE];
float i;

	printf("Enter a salary: %f", i);
	scanf("%f%*c", &array[i]);

	return(i);
}

/* Calculate pay rise */
void PayRise (int pay)
{

pay = i*.2

return;
}

/* Find the largest salary */
	max = array[0];

	for(i = 1; i < SIZE; i++)
	{ 
		if(array[i] > max)
			max = array[i];
	}

	
/* Print Results */
void PrintResults(int pay)
{

	printf("After a 20% pay rise: ", pay);
	printf("The largest salary is: ", max);

	return;
}

int main()
{

	int array[SIZE];
	float i;

	int pay;

	PrintResults(pay);

	return (0);
}

Recommended Answers

All 8 Replies

just try this..........

#include <stdio.h>

const int SIZE = 10;
int a[SIZE];

int main()
{
          int i;
          printf("Enter salaries");
          for (i=0;i<SIZE;i++)
          {
                printf("%d:  ",i);
                scanf(%f,&a[i]);
          }

          //updating salaries - with 20% increment
          for(i=0;i<SIZE;i++)
              a[i]=a[i]*1.2;
          
          //Finding max as well as well as printing new salaries also
          int pos=0;
          float max = array[0];
	
	printf("new salaries are : \n");

	for(i = 1; i < SIZE; i++)
	{ 
		
		printf("%f \n",a[i]);
		if(array[i] > max)
		{ 
                         max = array[i];
		      pos=i;
		}                    
	}
          printf("max salary payable is %f and in index %d : \n",max,pos);
	return (0);
}

Thanks for the code. I've been playing with it to try and work out each bit and am starting to understand. However, there were a few errors. I've managed to fix most of them but am still unable to compile because of this one:
Error E2451 pay.cpp 21: Undefined symbol 'array' in function main()

The new code:

#include <stdio.h>

const int SIZE = 10;
int a[SIZE];


int main()
{
          int i;
          printf("Enter salaries: ");
          for (i=0;i<SIZE;i++)
          {
                printf("%d:  ",i);
                scanf("%f",&a[i]);
          }

          for(i=0;i<SIZE;i++)
              a[i]=a[i]*1.2;
          
          int pos=0;
          float max = array[0];
	
	printf("New salaries are : \n");

	for(i = 1; i < SIZE; i++)
	{ 
		
		printf("%f \n",a[i]);
		if(array[i] > max)
		{ 
                         max = array[i];
		      pos=i;
		}                    
	}
          printf("The max salary is %f and is in index %d \n",max,pos);
	return (0);
}

Where is array[] defined? What line number?

I'm not sure if I defined it right. It compiles now, but doesn't work properly; it doesn't calculate the new salaries and doesn't display the correct 'max salary' and 'index'.

#include <stdio.h>

const int SIZE = 10;
int a[SIZE];

int main()
{

		int array[10];
		
          int i;
          printf("Enter salaries: ");
          for (i=0;i<SIZE;i++)
          {
                printf("%d:  ",i);
                scanf("%f",&a[i]);
          }

          for(i=0;i<SIZE;i++)
              a[i]=a[i]*1.2;
          
          int pos=0;
          float max = array[0];
	
	printf("New salaries are : \n");

	for(i = 1; i < SIZE; i++)
	{ 
		
		printf("%f \n",a[i]);
		if(array[i] > max)
		{ 
                         max = array[i];
		      pos=i;
		}                    
	}
          printf("The max salary is %f and is in index %d \n",max,pos);
	return (0);
}

ohh so sorry ...
please replace a[ with array[
as below--

#include <stdio.h>

const int SIZE = 10;
[B]float[/B] array[SIZE];

int main()
{
          int i;
          printf("Enter salaries");
          for (i=0;i<SIZE;i++)
          {
                printf("%d:  ",i);
                scanf(%f,&array[i]);
          }

          //updating salaries - with 20% increment
          for(i=0;i<SIZE;i++)
              array[i]=array[i]*1.2;
          
          //Finding max as well as well as printing new salaries also
          int pos=0;
          float max = array[0];
	
	printf("new salaries are : \n");

	for(i = 1; i < SIZE; i++)
	{ 
		
		printf("%f \n",a[i]);
		if(array[i] > max)
		{ 
                         max = array[i];
		      pos=i;
		}                    
	}
          printf("max salary payable is %f and in index %d : \n",max,pos);
	return (0);
}

previously I was useing a as array name and afterwords i used array for the same.
so that's why this mistake has happend...
one more thing i missed --- data type of this array..
it was int so please change your code with upper code and run... it would run fine..
i do not have c compiler in my machine thats why i can't tell you that this code is bug free or not ..

@ MooCrow -- please tell what changes you have done in your previous to previous code ... please

I can't remember what changes I made.. I know that the %f wasn't in quotes. I also just had to change the for=1 to for=0. That may have been it.

It now compiles, thank you. Last question, how do I get it to display only 2 decimal places?

New code:

#include <stdio.h>

const int SIZE = 10;
float array[SIZE];

int main()
{

          int i;
          printf("Enter salaries: ");
          for (i=0;i<SIZE;i++)
          {
                printf("%d:  ",i);
                scanf("%f",&array[i]);
          }

          for(i=0;i<SIZE;i++)
              array[i]=array[i]*1.2;
          
          int pos=0;
          float max = array[0];
	
	printf("New salaries are : \n");

	for(i = 0; i < SIZE; i++)
	{ 
		
		printf("%f \n",array[i]);
		if(array[i] > max)
		{ 
                         max = array[i];
		      pos=i;
		}                    
	}
          printf("The max salary is %f and is in index %d \n",max,pos);
	return (0);
}

@MooCrow....
Replace %f by %.2f in printf statement.

thank you :)

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.