Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,392 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,122 people online right now. Registration is fast and FREE... Join Now!




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

 
Reply to this topicStart new topic

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

chrisfields
20 Sep, 2006 - 06:21 AM
Post #1

New D.I.C Head
*

Joined: 20 Sep, 2006
Posts: 12


My Contributions
CODE

#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;
    }

}

User is offlineProfile CardPM
+Quote Post

Xing
RE: A Function To Find+Add And Sort Value In An Array
20 Sep, 2006 - 06:26 AM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
And what is your question?
User is offlineProfile CardPM
+Quote Post

chrisfields
RE: A Function To Find+Add And Sort Value In An Array
20 Sep, 2006 - 06:28 AM
Post #3

New D.I.C Head
*

Joined: 20 Sep, 2006
Posts: 12


My Contributions
I am trying to figure out how to use the conditional operator to sort a similar array.....
User is offlineProfile CardPM
+Quote Post

Xing
RE: A Function To Find+Add And Sort Value In An Array
20 Sep, 2006 - 06:34 AM
Post #4

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
Try to make use of brackets.
User is offlineProfile CardPM
+Quote Post

chrisfields
RE: A Function To Find+Add And Sort Value In An Array
20 Sep, 2006 - 06:43 AM
Post #5

New D.I.C Head
*

Joined: 20 Sep, 2006
Posts: 12


My Contributions
QUOTE(Xing @ 20 Sep, 2006 - 07:34 AM) *

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.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: A Function To Find+Add And Sort Value In An Array
20 Sep, 2006 - 06:52 AM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 45 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

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

http://www.concentric.net/~ttwang/sort/sort.htm
User is offlineProfile CardPM
+Quote Post

Xing
RE: A Function To Find+Add And Sort Value In An Array
20 Sep, 2006 - 06:59 AM
Post #7

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
QUOTE(chrisfields @ 20 Sep, 2006 - 08:13 PM) *
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
User is offlineProfile CardPM
+Quote Post

chrisfields
RE: A Function To Find+Add And Sort Value In An Array
20 Sep, 2006 - 07:47 AM
Post #8

New D.I.C Head
*

Joined: 20 Sep, 2006
Posts: 12


My Contributions
Thanks guys that was a great help!!!!!!
User is offlineProfile CardPM
+Quote Post

chrisfields
RE: A Function To Find+Add And Sort Value In An Array
21 Sep, 2006 - 02:12 PM
Post #9

New D.I.C Head
*

Joined: 20 Sep, 2006
Posts: 12


My Contributions
CODE
//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...... smile.gif

This post has been edited by Dark_Nexus: 21 Sep, 2006 - 02:42 PM
User is offlineProfile CardPM
+Quote Post

Xing
RE: A Function To Find+Add And Sort Value In An Array
21 Sep, 2006 - 04:35 PM
Post #10

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
CODE

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

You can replace this code with another swap function of yours
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:37AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month