GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Apr-2004, 19:11
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road

Using an array and finding the element number (subscript)


I am trying to find the element number in an array. I already found the minimum number, but I need to find the element number of that minimum number. Here's the code:
CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>

float minimum(float[], int);

int main()
{
	int a;
	float ticketPrices[6];

	for (a=0;a<=5;++a)		/*enter 6 ticket prices*/
	{
		printf("Please enter the ticket prices:   ");
		scanf("%f", &ticketPrices[a]);
	}
	for (a=0;a<=5;++a)
		printf("\nTickets prices that you entered are:   %.2f", ticketPrices[a]);

	printf("\nThe minimum value is:  %.2f", minimum(ticketPrices, 6));

	printf("\nThis is element number:  %d", );
	
	getch();
    
	return 0;
}

float minimum(float number[], int numEls)
{
	int a;
	float min=number[0];

	for (a=1;a<numEls;++a)
		if(min>number[a])
			min=number[a];

	return(min);
}
The printf statement that says "This is element number:" Not sure how to find the subscript number (element number). Do I need another variable in the For loop? I posted this on another group and a response I got back was to create another function. Do I really need to do this? The number[a] didnt work as this is the minimum number (smallest of the numbers that's entered by the user). Any ideas? I'm not that good at using pointers. Thanks for any info......Tommy
  #2  
Old 01-Apr-2004, 19:37
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Well, Tommy, it's quite obvious to me that you have already found out what element number you're looking for. If you look at your minimum function, you'll see that you have a counter (a) that ends up being used to set min to the minimum value in the array. All you need to do is grab the value of the counter from that function and print it to the screen.

Just to clarify, when I say to grab the value of the counter, I don't mean to use it to access the array. Here's what I'm saying you should do in code:

CPP / C++ / C Code:
printf("The element number of the minimum value is: %d", a);
All you need to do is extract the counter from the function so you can use it in a printf statement like that.
__________________
-Aaron
  #3  
Old 02-Apr-2004, 02:55
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by tommy69
I posted this on another group and a response I got back was to create another function. Do I really need to do this? The number[a] didnt work as this is the minimum number (smallest of the numbers that's entered by the user). Any ideas? I'm not that good at using pointers. Thanks for any info......Tommy
Not from me.... I did say you could write another function, but my main suggestions was to rewrite your minimum function to return a different value.
__________________

Age is unimportant -- except in cheese
  #4  
Old 02-Apr-2004, 07:32
meet_raman meet_raman is offline
Junior Member
 
Join Date: Mar 2004
Posts: 34
meet_raman is on a distinguished road

hmmmmmmmm


why cant we just print out both:
> the minimum element
> its position in array
in the minimum() function itself??

as aaron said "grab" the value, may be into another variable, each time "min" is updated.
later print both.

wud work?? i guess so.
  #5  
Old 02-Apr-2004, 08:48
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
Quote:
Originally Posted by WaltP
Not from me.... I did say you could write another function, but my main suggestions was to rewrite your minimum function to return a different value.

Walt,

The person I was talking about was tzuchan. I think there's an easier way than to create another function. Like creating a variable.
  #6  
Old 02-Apr-2004, 10:41
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Quote:
Originally Posted by tommy69
Walt,

The person I was talking about was tzuchan. I think there's an easier way than to create another function. Like creating a variable.
Did you read my post? Your answer is right in front of you.
__________________
-Aaron
  #7  
Old 02-Apr-2004, 17:23
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by aaroncohn
Did you read my post? Your answer is right in front of you.
An answer is right in front of you. Your suggestion (as written) returns two values from the function. A better way is simply return the index from the function. With that you have both the minimum value as well as the index.
__________________

Age is unimportant -- except in cheese
  #8  
Old 02-Apr-2004, 17:32
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
That's quite true, as you could access the minimum value of the array wherever you want, but you can only return one value from the function. Just return the index of the minimum item, then display it using the index instead of the min variable.
__________________
-Aaron
  #9  
Old 04-Apr-2004, 11:24
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
Quote:
Originally Posted by aaroncohn
That's quite true, as you could access the minimum value of the array wherever you want, but you can only return one value from the function. Just return the index of the minimum item, then display it using the index instead of the min variable.

Isn't the index of the minimum a? If it isn't, then I must not be too bright. Are you saying to return a? I have tried "return(a)" instead of "return(min)", but this doesnt work. I have also tried "return (min[a])" and I still can't get it. You guys are making it sound easy, but for me it isnt.
  #10  
Old 04-Apr-2004, 13:37
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Okay, here's what you want to do. You return the counter "a" when your function is done finding the minimum, then in main(), you can access the array with it to display the minimum. So your code would look like this...

CPP / C++ / C Code:
.
.
.
  printf("\nThe minimum value is:  %.2f", ticketPrices[minimum(ticketPrices,6)]); 
  // function returns a, which is then used as the array index

  printf("\nThis is element number:  %d", minimum(ticketPrices,6)); 
  // doing the function again is the lazy way around creating a new variable, but it gets the job done.
  getch();
    
  return 0;
}

__________________
-Aaron
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Extra null element in an array samtediou MySQL / PHP Forum 2 11-Dec-2003 11:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:17.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.