Thread: extremely confused

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    22

    extremely confused

    hi guys, workin on another program and you guessed it...stumped.
    i need to have this program present a menu that is defined in a function that takes in 10 names, 10 ages and 10 GPA's and stores them in 3 different arrays. Then, using functions and dependin on which menu selection the user makes, the arrays will be sorted, either alphabetically, in ascending order, or descending order.

    It wont compile. It has something to do with my printData function and call.

    really could use the help guys. thanks!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void printmenu ()
    {
    	printf("MENU\n1 - Sort by Name (Alphabetically)\n2 - Sort by Age (Ascending Order)\n3- Sort by GPA (Descending Order)\n4 - Quit\n");
    }
    void printData(char *name[],int age[],float gpa[],int n)
    {
    	for (n=0;n<10;n++)
    		printf("Name\tAge\tGPA\t\n========================\n%s\t%i\t%f\t",name[n],age[n],gpa[n]);
    }
    void sortByName(char *a[], int n)
    {
    	int i, j;
    	char *temp;
    	
    	for(i=0;i<n-1;i++)
    		for(j=i+1;j<n;j++)
    			if(strcmp(a[i],a[j]) > 0)
    			{
    				temp = a[i];
    				a[i] = a[j];
    				a[j] = temp; 
    			}
    }
    void sortByAge(int a[], int n)
    {
    	int i, j;
    	int temp;
    	
    	for(i=0;i<n-1;i++)
    		for(j=i+1;j<n;j++)
    			if(a[i] < a[j])
    			{
    				temp = a[i];
    				a[i] = a[j];
    				a[j] = temp;
    			}
    }
    void sortByGPA(int a[], int n)
    {
    	int i, j;
    	float temp;
    	
    	for(i=0;i<n-1;i++)
    		for(j=i+1;j<n;j++)
    			if(a[i] > a[j])
    			{
    				temp = a[i];
    				a[i] = a[j];
    				a[j] = temp;
    			}
    }
    
    main ()
    {
    	char *name[10]={"Gerrard","Reina","Torres","Kuyt","Babel","Carragher","Alonso","Skrtel","Mascherano","Benayoun"};
    	int age[10]={22,28,23,21,27,19,20,25,18,24};
    	float gpa[10]={4.00,3.54,3.21,3.11,2.45,2.67,3.92,2.14,3.76,3.01};
    	int selec;
    	
    	printmenu ();
    	scanf("%i",&selec);
    
    	if (selec>4)
    	{
    		switch (selec)
    		{
    		case1:sortByName(name,10);
    			printData (name,10);
    		break;
    		case2:sortByAge(age,10);
    			printData (age,10);
    		break;
    		case3:sortByGPA(gpa,10);
    			printData (gpa,10);
    		break;
    		case4:exit(0);
    		break;
    		default:printf("error");
    		break;
    		}
    	}
    	
    	return 0;
    }

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    printData (name,10);

    The arguments you are passing don't match the definition of the function.
    void printData(char *name[],int age[],float gpa[],int n)

    IE you probably want something like

    printData(name, age, gap, 10);
    Last edited by valaris; 04-24-2009 at 09:17 AM.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    yeah i thought that was what it was, but im messing around with it and cant figure out how to pass all three arrays through??

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    It would be helpful to see what the compiler errors you get.

    I do know that the brackets '[]' in your function declarations in the parameters are not necessary, but I'm not sure if they would generate a compiler error.

    I also think that your declaration of name is incorrect. I think you'd need a 2d array since you want an array of character arrays. So you need something like char name[10][25] which allows for 10 names of up to 25 characters.

  5. #5
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by hockey1 View Post
    yeah i thought that was what it was, but im messing around with it and cant figure out how to pass all three arrays through??
    I edited my original post to show you.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    ok heres`'s what ive got it down to...

    [13]ss015_eepc13> gcc program7.c
    program7.c: In function `main':
    program7.c:77: warning: passing arg 1 of `sortByGPA' from incompatible pointer type

    Code:
    #include <stdio.h>
    
    #include <string.h>
    
    
    
    void printmenu ()
    
    {
    
    	printf("MENU\n1 - Sort by Name (Alphabetically)\n2 - Sort by Age (Ascending Order)\n3- Sort by GPA (Descending Order)\n4 - Quit\n");
    
    }
    
    void printData(char *name[],int age[],float gpa[],int n)
    
    {
    
    	for (n=0;n<10;n++)
    
    		printf("Name\tAge\tGPA\t\n========================\n%s\t%i\t%f\t",*name[n],age[n],gpa[n]);
    
    }
    
    void sortByName(char *a[], int n)
    
    {
    
    	int i, j;
    
    	char *temp;
    
    	
    
    	for(i=0;i<n-1;i++)
    
    		for(j=i+1;j<n;j++)
    
    			if(strcmp(a[i],a[j]) > 0)
    
    			{
    
    				temp = a[i];
    
    				a[i] = a[j];
    
    				a[j] = temp; 
    
    			}
    
    }
    
    void sortByAge(int a[], int n)
    
    {
    
    	int i, j;
    
    	int temp;
    
    	
    
    	for(i=0;i<n-1;i++)
    
    		for(j=i+1;j<n;j++)
    
    			if(a[i] < a[j])
    
    			{
    
    				temp = a[i];
    
    				a[i] = a[j];
    
    				a[j] = temp;
    
    			}
    
    }
    
    void sortByGPA(int a[], int n)
    
    {
    
    	int i, j;
    
    	float temp;
    
    	
    
    	for(i=0;i<n-1;i++)
    
    		for(j=i+1;j<n;j++)
    
    			if(a[i] > a[j])
    
    			{
    
    				temp = a[i];
    
    				a[i] = a[j];
    
    				a[j] = temp;
    
    			}
    
    }
    
    
    
    main ()
    
    {
    
    	char *name[10]={"Gerrard","Reina","Torres","Kuyt","Babel","Carragher","Alonso","Skrtel","Mascherano","Benayoun"};
    
    	int age[10]={22,28,23,21,27,19,20,25,18,24};
    
    	float gpa[10]={4.00,3.54,3.21,3.11,2.45,2.67,3.92,2.14,3.76,3.01};
    
    	int selec;
    	int n=10;
    
    	
    
    	printmenu ();
    
    	scanf("%i",&selec);
    
    
    
    	if (selec>4)
    
    	{
    
    		switch (selec)
    
    		{
    
    		case1:sortByName(name,n);
    
    			printData (name,age,gpa,n);
    
    		break;
    
    		case2:sortByAge(age,n);
    
    			printData (name,age,gpa,n);
    
    		break;
    
    		case3:sortByGPA(gpa,n);
    
    			printData (name,age,gpa,n);
    
    		break;
    
    		case4:exit(0);
    
    		break;
    
    		default:printf("Error - Retry");
    
    		break;
    
    		}
    
    	}
    
    	
    
    	return 0;
    
    }

  7. #7
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Your prototype is void sortByGPA(int a[], int n), but you are passing in floats. Change sortByGPA to void sortByGPA(float a[], int n)

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    genious! now it compiles fine but i think my switch is messed up? anything i put in doesnt seem as though its actually running the sort, let alone print the results..

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    ok i was missing the ' ' in my switch statement, however its still not working

  10. #10
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    try:

    Code:
    scanf("%d", &selec);
    
    switch(selec)
    {
       case 1:
       //stuff
       break;
    }
    Verify that you actually get into one of your sort functions. You can do this with a debugger, or if you dont know how to use one at the top of your function print out something like "got 1", etc. Tell me where your program gets to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 07:32 PM
  2. Confused
    By jeev2005 in forum C Programming
    Replies: 5
    Last Post: 06-01-2006, 02:04 PM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM