Please help me!

peps
peps used Ask the Experts™
on
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
ok i helped u first .. give me points now

Author

Commented:
#include <stdio.h>

#define SIZE 100

 int menu(int);
 void printArray(long int []);
 void sortArray(long int []);    
 void sortdeArray(long int []);
 void printdeArray(long int []);

 
     
main()
{
     long int fArray[SIZE]={0};
     int a=0;
     int option;    
     while (option!=5)
     {

          option=menu(option);
     
          if (option==1)
          {
               printf("Please enter a phone number:\n");
               scanf("%d", &fArray[a]);
               a++;
          }
         
          if (option==2)
          {
               printArray(fArray);    
          }
          if (option==3)
          {
               sortArray(fArray);
               printArray(fArray);
          }
          if (option==4)
          {
               sortdeArray(fArray);
               printdeArray(fArray);
          }
          if (option==5)
          {
               printf("\nThank you for coming. GOODBYE!\n");
          }
     }
}
void printArray(long int fArray[])
{
     int i;
     int count=1;
     printf("\n");
     for (i = 0; i<100; i++)
     {  if(fArray[i]!=0)
          {printf("%d.  %u \n", count, fArray[i]);
           count++;
          }
     }
   printf("\n");
}

void printdeArray(long int fArray[])
{
     int i;
     int count=100;
     printf("\n");
     for (i = 0; i<100; i++)
     {  if(fArray[i]!=0)
          {printf("%d.  %u \n", count, fArray[i]);
           count--;
          }
     }
   printf("\n");
}

void sortArray(long int fArray[])
{
     int temporaryCell, nextPass, whichNum, yesSwap = 0;
     for (nextPass = 0; nextPass<SIZE - 1; nextPass++)
     {  
          if (yesSwap == 0)
          {
               yesSwap = 1;
               
               for( whichNum = 0; whichNum < SIZE-1 ; whichNum++)
               {  
                   
                   if(fArray[whichNum] >fArray[whichNum + 1])
                    {
                         temporaryCell = fArray[whichNum];
                         fArray[whichNum] = fArray[whichNum + 1];
                         fArray[whichNum + 1] = temporaryCell;
                         yesSwap = 0;                      
                    }
               }
     
          }
      else
               break;
     }
}

void sortdeArray(long int fArray[])
{
     int temporaryCell, nextPass, whichNum, yesSwap = 0;
     for (nextPass = 0; nextPass<SIZE - 1; nextPass++)
     {  
          if (yesSwap == 0)
          {
               yesSwap = 1;
               
               for( whichNum = 0; whichNum < SIZE-1 ; whichNum++)
               {  
                   
                   if(fArray[whichNum] < fArray[whichNum + 1])
                    {
                         temporaryCell = fArray[whichNum+1];
                         fArray[whichNum+1] = fArray[whichNum];
                         fArray[whichNum] = temporaryCell;
                         yesSwap = 0;                      
                    }

               }
          }
      else
               break;
     }
}

int menu(int option)
{
     if (option <5 || option >0)
     {
          printf ("1. Add a Phone Number\n");
          printf ("2. View Phone Numbers\n");
          printf ("3. Sort Assending Phone Numbers\n");
          printf ("4. Sort Desending Phone Numbers\n");
          printf ("5. Exit\n");
          printf ("Please choose an option from 1-5\n");
          scanf ("%d",&option);
     }
     return option;
}


I need it to check if the number is already iin the array, if it is then it asks for a new number....


Please help me I am going crazy thank you in advanced!!!!!
Commented:

How about this? (of course you need to declare the variables)

if (option==1)
{  
    printf("Please enter a phone number:\n");
    scanf("%d", &tmp);
   
    /* Check for number. */
    for(i=0; i<SIZE; i++) {
        if(fArray[i]== tmp) {
            printf("Please enter NEW phone number:\n");
            scanf("%d", &fArray[i]);
        }
        else {
           fArray[a]=tmp;
           a++;
        }
    }
}
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
Quick comments:

o   main() is wrong. It should be int main(void) for this  
    code.
o   You haven't initialized the "option" variable before
    entering the while loop.
o   You are not testing the value of the variable "a"
    against SIZE. Thus if more than 100 numbers are added,
    interesting things will happen.
o   You are missing a return statement at the end of
    main().
o   the menu routine does not require a calling argument.
    It prompts for user input. That input is returned to
the caller.
Obviously a homework assignment so here are some hints:


Use a temporary variable to hold the input phone number. Use that to compare against the numbers already in the array. If it is a new number, add it to the array and increment the index (variable "a"). Otherwise print an error mesg, set
printf("Please enter a phone number:\n");
              scanf("%d", &fArray[a]);
           
              a++;

Commented:
Gaak! Submit key hit by accident. Ach! I hate when that happens...

As I was suggesting...

The menu routine should include a prompt for and return a user input value. It does not require an input argument. In your version, you are checking for a valid option number /before/ the user has input the value.

It is a good idea to use the macro definition for a array size rather than the constant for code that needs to check array index limits. In your code you have lines such as

         for (i = 0; i < 100; i++)

Change that to

         for (i = 0; i < SIZE; i++)

You have declared fArray as "int fArray[SIZE]", so it is better to be consistent in the for loops by using the same macro define.

I've not spent time checking much else in your code.

With respect to your original question...

In pseudo code...

if (option == 1)
{
    if (fArray index == SIZE)
    {
         print error message
    }
    else
    {
        temp = user supplied phone #
        scan fArray for temp
        if (temp value exists in fArray)
        {
            print error message
        }
        else
        {
            add temp to fArray
            increment fArray index
        }
    }
}

To scan fArray use a for loop. With each iteration, i, of the loop compare the value of fArray[i] to temp. If equal, then break out of the loop. Outside of the loop check the value of "i". If it is less than SIZE, the temp value exists in fArray.    

[Sorry for the premature Submit]
no commemts
no question available or i cant read blank lines
Top Expert 2006

Commented:
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with 20:30 split between etenes  and ewest with A grade

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Sunny
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial