having problem in largest number in arrray

March 25, 2010 at 00:19:57
Specs: Windows XP
while comipling this program i.e.,to find the largest number in the array... i m having these errors

Line10: Expression syntax
Line 10: Undefined symbol i
Line 10: Statement missing
Line 20: 'tos' is assigned a value that is never used
Line 20: 'data' is assigned a value that is never used


and the code is----

#include<stdio.h>
#include<conio.h>

main()
{
	int data[6]  = {2,3,4,5,8,7};
	int tos=1;

	clrscr();
	for(int i=0;i<=6;i++)
	{
		if(data[tos] < data[i])
		{
			tos = i;
		}
	}
	printf("Largest number in list is : ",data[tos]);
	getch();
	return 0;

}


PLEASE HELP


See More: having problem in largest number in arrray

Report •


#1
March 25, 2010 at 02:02:33
You must be very careful with you array indexes, you have an array of six ints and the for loop is trying to access seven.

int i=0;i<=6;i++

Should be either:

int i=0;i<=5;i++

or

int i=0;i<6;i++

Also your printf statement doesn't display the value, I guess you may want to add "%d" to it.


Report •

#2
March 25, 2010 at 06:25:29
i also tried this but not able to remove the errors----
#include<stdio.h>

main()
{
	int data[6]  = {2,3,4,5,8,7};
	int tos=1;

	for(int i=0;i<=5;i++)
	{
		if(data[tos] < data[i])
		{
			tos = i;
		}
	}
	printf("Largest number in list is : %d ",data[tos]);
}

Still having the same errors


Report •

#3
March 25, 2010 at 06:36:38
but surprisingly, when i run this code for the smallest number in the array it runs perfectly

#include<stdio.h>

main()
{
	int data[6] = {9,3,5,2,4,7};
	int tos;

	for(int i=0;i<=5;i++)
	{
		if(data[tos] > data[i])
		{
			tos =i;
		}
	}
	printf("the smallest number in the list is: %d",data[tos]);
}


Report •

Related Solutions

#4
March 25, 2010 at 07:45:44
That's strange it seemed to work fine for me with that one change.

It's a really bad idea to use variables you haven't initialized with a value, you can end up with any number that just happens to be in memory. Intermittent bugs and crashes can result.


Report •


Ask Question