help plzzzzzzz in c language

Discussion in 'Programming' started by rock2010, Apr 15, 2008.

  1. #1
    hi everyone

    i need help in those problems


    Question 1:
    Write a program that checks if an integer value entered by the user is present in a file or not. If present, the program should indicate the order of this value in the file i.e. it is value number 1, or value number 20 etc…
    Your program should also check the file not found error case. In the first run shown below, the file was not in the folder. In the remaining runs, the file is there and the messages displayed by the program are shown. The number of values in the file is unknown. Assume that they are not repeated i.e. each value is written only once.



    Note: You should stop searching once you find the value you are looking for. You can use a logical variable and keep looking for the value of the user as long as it is not found and you did not reach the end of the file. Both conditions should be included in the loop.

    Question 2:

    Write an interactive program that displays a menu ( use do-while) containing many choices as shown below:
    1- split a real number
    2- check if a number is perfect square
    3- check if a number is prime
    4- check if a number is odd
    5- Exit
    You have to write 5 functions as indicated below:
    Function 1 to display the choices shown above.
    Function 2 to receive a real value and return its whole part and decimal part separately (not print them); split function
    Function 3 is a logical function that receives a positive integer number and returns 1 if it is perfect square, 0 otherwise. Perfect squares are 4,9,16,25 etc…
    Function 4 is a logical function to receive a positive integer number and returns 1 if it is prime, 0 otherwise.
    Function 5 is also a logical function to check if an integer number is odd or not.
    In the main function, your program will ask the user to select a choice then, read the data and call the right function and print the right message. Sample runs of your programs are shown below.






    for sample output

    _ http://myfreefilehosting.com/f/7d74d2cea0_0.14MB


    thanks for help .
     
    rock2010, Apr 15, 2008 IP
  2. James_P

    James_P Peon

    Messages:
    413
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Not that you're asking anyone to help you with your homework or anything... lol.
     
    James_P, Apr 15, 2008 IP
  3. Arkserver

    Arkserver Banned

    Messages:
    2,533
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Pretty easy assignment anyway. :)
     
    Arkserver, Apr 15, 2008 IP
  4. Swizznezz

    Swizznezz Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    show a little effort, no one will do homework for you.
     
    Swizznezz, Apr 15, 2008 IP
  5. rock2010

    rock2010 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks for interactive and intrests
     
    rock2010, Apr 15, 2008 IP
  6. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #6
    
    fp=fopen("file.txt","r");
    while(fscanf(fp,"%d",&i)!=EOF) {
    if (i==number) {
    break;
    }
    }
    fclose(fp);
    
    Code (markup):
     
    it career, Apr 16, 2008 IP
  7. rock2010

    rock2010 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thank you verry much

    i need the other code also plzzzzzzz
     
    rock2010, Apr 18, 2008 IP
  8. rock2010

    rock2010 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    can you check please , what is the rong with my code ?!!

    #include<stdio.h>

    int main () {

    int x , z ;

    FILE *infile ;

    printf("enter the value you want") ;
    scanf ("%d",&x) ;

    infile = fopen("data.txt", "r") ;

    fscanf(infile,"%d",&z) ;

    while ( fscanf(infile,"%d",&z) !=EOF){

    if(z==x){
    printf ("the value exist in the file") ;

    break ;

    else
    printf("does not match") ;
    }
    }

    fclose (infile) ;

    return 0 ;


    }
     
    rock2010, Apr 23, 2008 IP
  9. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #9
    Move else out of if , wrong C syntax.
     
    it career, Apr 23, 2008 IP
  10. rock2010

    rock2010 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    thanks
    i removed else but when i enter a value which exists in the input file the program stops

    try my code and you will see it
     
    rock2010, Apr 23, 2008 IP
  11. rock2010

    rock2010 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    it is my new code

    #include<stdio.h>

    int main () {

    int x , z ;

    FILE *infile ;

    printf("enter the value you want") ;
    scanf ("%d",&x) ;

    infile = fopen("data.txt", "r") ;

    fscanf(infile,"%d",&z) ;

    while ( fscanf(infile,"%d",&z) !=EOF){

    if(z==x){
    printf ("the value exist in the file") ;

    break ; }


    }

    fclose (infile) ;

    return 0 ;


    }
     
    rock2010, Apr 23, 2008 IP
  12. mikelaw

    mikelaw Peon

    Messages:
    148
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #12
    It will stop because you put a break; in:

    if(z==x){
    printf ("the value exist in the file") ;

    break ; }


    }


    which makes the while loop quit when you find a match. Try:


    if(z==x){
    printf ("the value exist in the file") ;
    }
     
    mikelaw, Apr 23, 2008 IP
  13. rock2010

    rock2010 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    it is the same
    doesn`t make any different
     
    rock2010, Apr 23, 2008 IP
  14. rock2010

    rock2010 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    who has any idea about this function
    Function 2 to receive a real value and return its whole part and decimal part separately (not print them); split function ??
     
    rock2010, Apr 23, 2008 IP
  15. mikelaw

    mikelaw Peon

    Messages:
    148
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Look up trunc or truncf in your IDE's help.
     
    mikelaw, Apr 23, 2008 IP