Thread: Help with C programming problems

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    72

    Thumbs up Help with C programming problems

    here i think everyone who finds difficulties should post any C programing problem that we would try to discuss and solve. This would make it easy for users to post their request in one threat instead of hundreds of them.

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    72
    i begin with my problem in fact here's what the exercice says:"Write a program that reads an array of strings, sorts the array using the insertion sort, and displays the sorted array. The array can be declared as follows:char str[SIZE][N]; where N is defined as a preprocessing directive
    make sure you use appropriate string functions such as gets to read a string, and strcpy to perform an assignment.
    It may sound easy but i hope i will get a response

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    72
    it's not a homework its just an exercice i found and i wanted to train with it as i only discover strings in the course! thanks

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    72
    Code:
     /* 
    #include<stdio.h>
    #define SIZE 5
    #include <stdio.h>
    
    void print_array(int *);
    void read_array(int []);
    void sort_array(int *);
    int search(int, int[]);
    void choice_operation(int []);
    
    	int main(){
               int ar[SIZE];			//array declaration (SIZE is constant)
               
    		   read_array(ar);		//read array elements
               sort_array(ar);		//sort the array
               choice_operation(ar);	//display a list of operations
               system("PAUSE");
               return 0;
    }
    void read_array(int x[]){
           int i;
         printf(" Enter the array: ");
    	 for (i = 0 ; i < SIZE ; i++)
            scanf("&#37;d",&x[i]);                         
    }
    
    void sort_array(int ar[])
    {
    
    	int i, j;
    	
    	int tmp;
    	
    	for (j=1; j < SIZE; j++)
        {
           tmp = ar[j];
           
           for (i = j; (i > 0) && (ar[i-1] > tmp); i--)
           
                 ar[i] = ar[i-1];
                 
           ar[i] = tmp;
        }
    	 
    printf("                Array sorted succesfully.\n");
    
    printf("\n");
    
    
    }
    
    
    int search (int v,int x[]){
             int i;         for (i = 0 ; i < SIZE ; i++){
                                if (x[i] == v)   
                                   return(i); 
                       }
                       return(-1);
    }
    void choice_operation(int x[]){
    	
                                   int i,y=0,sum = 0,a = 0,v;
                                   do{
                                   printf("To display the max and the min of the array, enter 1\n");
                                   printf("To know a position of a value, Enter 2\n");
                                   printf("To find the average, Enter 3\n");
                                   printf("To list the elements of the array, Enter 4\n");
                                   printf("To exit, Enter any other character.\n");
                                   scanf("%d",&y);
                                   
    							   	switch(y){
                                   case 1:
                                        printf("the max is %d, and the min is %d\n",x[0],x[SIZE-1]);
                                        break;
                                   case 2:
                                         printf("Enter a value to know its position within the array:\n");
                                         scanf("%d",&v);
                                         a = search(v,x);
                                         if(a != -1)
                                              printf("the positions of %d is %d\n",v,a);
                                         else
                                              printf("this element does not belong to the array\n");
                                         break;
                                   case 3:
                                        for (i = 0 ; i < SIZE ; i++)
                                                 sum += x[i];
                                        printf("The average is %d\n",(sum / SIZE));
                                        break;
                                   case 4:
                                        print_array(x);
                                             break;
                                   default:
                                           printf("The program ends.\n");
                                           return 0;
    							   	}
                                        
             }
                                   while(y=1);
     }
      
       
       
          
    
    
    void print_array(int A[])
    {
    	int i;
    	printf("\n");
    	printf("     The sorted array is the following \n");
    	printf("\n");
    	for(i = 0; i < SIZE ;i++)
    		printf("     %d\n", A[i]);
    	
    }
    */

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    72
    the algorithm above in a program that reads, sort by insertion sort, give the position of a chosen number in the array.... in order to let the user do as much operations as he wants i made a"do while" loop still it works but i don't understand why i had to make while(y=1) to make the menu reapear any suggestings?

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by overlord21 View Post
    here i think everyone who finds difficulties should post any C programing problem that we would try to discuss and solve. This would make it easy for users to post their request in one threat instead of hundreds of them.
    Also, instead of having libraries and books, we should write down all of human knowledge on a single huge piece of paper, that will obviously make everything easier.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    72
    i don't know if its sarcastic, but you surely know that we are only talking about C programming(one topic). So this board won't be overcrowd....

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's sarcasm, because it would be the same as putting together all our knowledge into a single huge piece of paper.
    In other words - it's bad. What happens if thousands of people start posting their problems in the same topic? Replies all the time, forth and back to each problem. It would become quite the mess.
    There's a reason why there's a single topic for everything.

    And don't 5 five times in a row. If you've been to other forums, you would know that double posting is usually bad, triple posting sneered at and 4 times or more usually unheard of, in many cases (unless it's a special type of thread).

    There's an edit button. I do suggest you learn how to use it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    i don't know if its sarcastic, but you surely know that we are only talking about C programming(one topic). So this board won't be overcrowd....
    Neither will there be overcrowding if each topic had its own thread.

    Here's a chance to try again: post your question in a new thread. Use an appropriate title and get straight to the point. State what you are trying to do, what you actually did, and how does it not work.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Perhaps you should read the dictionary, for 2 reasons at least.

    Imagine if the words were unordered, and their definitions were also unordered and on different pages. Now if I asked you to find the meaning of "stupid" in 500,000 words it'd take you hours. Not to mention you'd have to know the meaning of "stupid" to find it since the word and it's definition don't match, nor are in order.

    I don't know if you're having a laugh, or just looking for a post count. Presumably you're a human, you categorize everything into an object, if you didn't you wouldn't work.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    72
    humans are such criticizing beings

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    tHAT IS HOW WE IMPROVE!! by criticizing!

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    3
    HELP PLEASE

    I have a project to do where the teacher gives us a .dat file and we have to put it into an array. this .dat file will be added to the directory the run with the program as a.out < scores.dat.


    Please tell me how to do this.

    Assignment:
    You job is to write a program that will read in the golf scores of several players in a golf game. You will then display some statistics about the scores. You will generate an average score and list those players who scored below the average (a good thing!) You will also provide the functionality to show the winning score. The player with the lowest score is the winner.

    Below is the main() function you must use in your program:
    int main()
    {
    int scoreList [MAX_PLAYERS]; /* Array to store golf scores */
    int numberOfPlayers = 0; /* Total number of scores entered */
    float averageScore = 0.0; /* Average golf score */
    int winningScore = 0; /* The lowest score */

    printf("\nWelcome to CMSC104's Golf Statistics Program!\n\n");

    /* Fill array with golf scores */
    numberOfPlayers = FillGolfScoreList (scoreList);

    Thankyou

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) It's better to start a new thread.
    And 2) No, we're not going to help you unless you can show that you are capable of doing this. Start with as much as you can, show it and ask specific questions you do not understand. We can guide you, but we cannot give you the answers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by programnewbie View Post
    HELP PLEASE
    REad this and this
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM

Tags for this Thread