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

Page 1 of 1

## 9 Replies - 1597 Views - Last Post: 21 September 2006 - 05:35 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=18785&amp;s=48a4f56c01e2679c933d4afa9bdd1552&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 chrisfields

Reputation: 0
• 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//

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);

scanf("%d",&num2);

if(num2!=0)
{
q=SizeOfArray;

populate(numbers,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

Reputation: 19
• 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

### #3 chrisfields

Reputation: 0
• 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.....

### #4 Xing

Reputation: 19
• 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.

### #5 chrisfields

Reputation: 0
• 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

Xing, 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.

### #6 Jayman

• Student of Life

Reputation: 419
• 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

### #7 Xing

Reputation: 19
• 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

chrisfields, 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

### #8 chrisfields

Reputation: 0
• 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!!!!!!

### #9 chrisfields

Reputation: 0
• 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

### #10 Xing

Reputation: 19
• 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