4 Replies - 804 Views - Last Post: 17 September 2011 - 04:31 AM Rate Topic: ***-- 2 Votes

#1 n00bprogrammer89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-September 11

Help with short array-sort C program?

Posted 16 September 2011 - 07:33 PM

Hey guys. I'm trying to help a friend with a C program but have very little knowledge of it. I figured this would be a great place to come. I have started the program, but can't figure out how to store things into the array then sort it?

The program has to read in player records from the keyboard, which I believe I have done correctly. Then it must save the info into an array, then sort it in ascending order by userid and print it. I believe I am halfway-right about this. Could someone with more experience please help him/me out?

The player records read in as follows

600 Smith John 34 53 22
200 Jones Jim 22 44 56
300 Chappelle Dave 22 48 12



Here is the code I have

struct Player

{

int numOfRecords; //Stores the total number of records
char userid[10];
char last[10];
char first[10];
int ties;
int losses;
int wins;


printf ("Enter the number of records: \n");
scanf("%d", &numOfRecords);// stores total number of records

int x;
for (x = 0; x < numOfRecords; x++)

{

printf("Enter the record data:\n");

scanf("%s%s%s%d%d%d", userid, last, first, &wins, &losses, &ties);

}


}


edit by ishkabible: please use code tags when posting code. like so, :code: thank you :)

This post has been edited by ishkabible: 16 September 2011 - 07:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with short array-sort C program?

#2 n00bprogrammer89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-September 11

Re: Help with short array-sort C program?

Posted 16 September 2011 - 10:12 PM

Hey guys. I'm trying to help a friend with a C program but have very little knowledge of it. I figured this would be a great place to come. I have started the program, but can't figure out how to store things into the array then sort it?

The program has to read in player records from the keyboard, which I believe I have done correctly. Then it must save the info into an array, then sort it in ascending order by userid and print it. I believe I am halfway-right about this. Could someone with more experience please help him/me out?

The player records read in as follows

600 Smith John 34 53 22
200 Jones Jim 22 44 56
300 Chappelle Dave 22 48 12



Here is the code I have

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>

Struct Player
{
int numOfRecords; //Stores the total number of records
int userid;
char last[10];
char first[10];
int ties;
int losses;
int wins;
};

printf("Enter the number of records: \n");
scanf("%d", &numOfRecords);// stores total number of records

int x;
for(x = 0; x < numOfRecords; x++)

{

printf("Enter the record data:\n");

scanf("%d%s%s%d%d%d", &userid,&last,&first,&wins,&losses,&ties);

}


printf("%d%s%s%d%d%d\n", userid, last, first, wins, losses, ties);
}





Any help is greatly appreciated!!!
Was This Post Helpful? 0
  • +
  • -

#3 Ajgilzean   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 27-July 10

Re: Help with short array-sort C program?

Posted 16 September 2011 - 11:37 PM

I will have too much trouble explaining this over text. So here you go my friend.

http://www.youtube.c...ubble+sort&aq=f
Was This Post Helpful? 0
  • +
  • -

#4 rdhc1330   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 136
  • Joined: 01-August 11

Re: Help with short array-sort C program?

Posted 17 September 2011 - 01:40 AM

I would tend towards using a function to be honest, would be a lot easier to sort your User IDs in one and then return to print them in order...
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Help with short array-sort C program?

Posted 17 September 2011 - 04:31 AM

Why are you doing your friend's homework?

Also merged duplicate topics. Please do not create a new topic on the same subject as one you already have!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1