9 Replies - 1456 Views - Last Post: 21 September 2006 - 05:35 PM Rate Topic: -----

#1 chrisfields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 06

A Function To Find+Add and Sort Value In An Array

Posted 20 September 2006 - 07:21 AM

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

const int SizeOfArray =10;//array size constant//

int numbers[SizeOfArray];//array for number list//

int FindAndAdd(int[],int,int,int);//function prototype//

void populate(int[],int);//functionp prototype//

int main()
{
	int q=0,num1,num2;

	printf("\nPlease enter ther value for which you would like to search:");
	scanf("%d",&num1);

	printf("\nPlease enter the value to add:");
	scanf("%d",&num2);

	if(num2!=0)
	{
		q=SizeOfArray;
	
		populate(numbers,q);

		FindAndAdd(numbers,num1,num2,q);
	}

	if(num2=0)
	{
		printf("\nThe Sum is:%d",num2);
	}
	
	getch();
	
	return 0;
}

//function definition//
void populate(int array[],int size)
{
	int a;
	
	printf("\n");
	printf("Display List:");
	for(a=0;a<size;a++)
	{
		array[a]=a;
		printf("%d ",array[a]);
	}
}
//funtion definition//
int FindAndAdd(int array[],int x,int y,int size)
{
	int a=0,b=0;
	for(a=0;a<size;a++)
	{
		(array[a]==x)?b=(array[a]+y):array[a]=a;//conditional operator searches the list//
		
	}
	if(b!=0)
	{
		printf("\n\nThe Sum is:%d",B);
		return b;
	}

	if(b==0)
	{
		printf("\n\n\aSorry!!The Number Entered Is Not The List");
		return -1;
	}

}


Is This A Good Question/Topic? 0
  • +

Replies To: A Function To Find+Add and Sort Value In An Array

#2 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: A Function To Find+Add and Sort Value In An Array

Posted 20 September 2006 - 07:26 AM

And what is your question?
Was This Post Helpful? 0
  • +
  • -

#3 chrisfields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 06

Re: A Function To Find+Add and Sort Value In An Array

Posted 20 September 2006 - 07:28 AM

I am trying to figure out how to use the conditional operator to sort a similar array.....
Was This Post Helpful? 0
  • +
  • -

#4 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: A Function To Find+Add and Sort Value In An Array

Posted 20 September 2006 - 07:34 AM

Try to make use of brackets.
Was This Post Helpful? 0
  • +
  • -

#5 chrisfields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 06

Re: A Function To Find+Add and Sort Value In An Array

Posted 20 September 2006 - 07:43 AM

View PostXing, on 20 Sep, 2006 - 07:34 AM, said:

Try to make use of brackets.


in other words i need help creating a function that will sort values in an array............a simple one would help i would edit it to make it work for my program.
Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: A Function To Find+Add and Sort Value In An Array

Posted 20 September 2006 - 07:52 AM

You can find information and code examples on sorting algorithms at the following link.

http://www.concentri...g/sort/sort.htm
Was This Post Helpful? 0
  • +
  • -

#7 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: A Function To Find+Add and Sort Value In An Array

Posted 20 September 2006 - 07:59 AM

View Postchrisfields, on 20 Sep, 2006 - 08:13 PM, said:

in other words i need help creating a function that will sort values in an array............a simple one would help i would edit it to make it work for my program.

Have a look at different sort algorithms and their implementations here
Was This Post Helpful? 0
  • +
  • -

#8 chrisfields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 06

Re: A Function To Find+Add and Sort Value In An Array

Posted 20 September 2006 - 08:47 AM

Thanks guys that was a great help!!!!!!
Was This Post Helpful? 0
  • +
  • -

#9 chrisfields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 06

Re: A Function To Find+Add and Sort Value In An Array

Posted 21 September 2006 - 03:12 PM

//function definition//

int SumSort(int array[],int size)
{
int minval=0,tempval=0,a=0;
	for(a=0;a<size;a++)
	{
		minval=a;
		for(int y=a+1;y<size;y++)
		{
			if(array[y]<array[minval])
				{
					minval=y;
				}
		}
		tempval=array[a];
		array[a]=array[minval];
		array[minval]=tempval;
		printf("\n%d",array[a]);
	}
	return 0;
}



this is the code i used for the sort...... :)

This post has been edited by Dark_Nexus: 21 September 2006 - 03:42 PM

Was This Post Helpful? 0
  • +
  • -

#10 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: A Function To Find+Add and Sort Value In An Array

Posted 21 September 2006 - 05:35 PM

tempval=array[a];
array[a]=array[minval];
array[minval]=tempval;

You can replace this code with another swap function of yours
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1