473,326 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

2D arrays (urgent)

Hi,
I implemented a solution for a homework assignment and everytime i run my program it crashes. could someone tell me what i'm doing wrong?
I so confused what to pass into the functions when using 2D arrays.

Assignment Q:
Implement version 1 of the string sort program
Implement the string sort program based on the following main( ) function.
int main()
{
static char input[MAX_SIZE][MAX_STRLEN + 1];
int size;

size = GetInput(input, MAX_SIZE);
StrSort(input, size);
StrPrint(input, size);
return(0);
}

Read each word using scanf( ) and store the words one on each “row” of the 2-D array. Make the program a filter, just reading from the standard input and writing to the standard output. Prompt or instructions to the user should not be used. Isolate the read string and print string components of the program in separate functions.


My code:
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. //Manifest constants
  5. #define MAX_SIZE 20
  6. #define MAX_STRLEN 20
  7.  
  8. //Function Prototypes
  9. int GetInput(static char*, int);
  10. void StrSort(static char string, int size);
  11. void StrPrint(static char input, int size);
  12.  
  13. int main (void)
  14.     {
  15.     static char input[MAX_SIZE][MAX_STRLEN + 1];
  16.     int size;
  17.  
  18.     size = GetInput(input, MAX_SIZE);
  19.     StrSort(input, size);
  20.     StrPrint(input, size);
  21.     return(0);
  22.     }
  23.  
  24. /*************************************************************************
  25. *Function: StrSort
  26.  
  27.   This function sorts a 2d array of strings
  28.  
  29.   ***********************************************************************/
  30. void StrSort(static char *string, int size)
  31.     {
  32.     char *plist[MAX_SIZE];
  33.     char *temp;
  34.     int i,j;
  35.     for(i=0; i<size;i++)
  36.         plist[i] =  string[i];
  37.  
  38.     // Sort the array
  39.     for (i=0; i<size; i++)
  40.         for (j=size-1; j>i; j--)
  41.             if (plist[i]>plist[j]) {        // We want ascending order, exchange these elements
  42.                 temp=plist[i];
  43.                 plist[i]=plist[j];
  44.                 plist[j]=temp;
  45.             }
  46.  
  47.  
  48.     }
  49.  
  50. /*************************************************************************
  51. *Function: StrPrint
  52.  
  53.  This Functions prints out a 2D array of strings
  54.  
  55.     ********************************************************************/
  56.  
  57. void StrPrint(static char string[MAX_SIZE][MAX_STRLEN+1], int size)
  58.     {
  59.         int i;
  60.  
  61.     // Display the student grades - after sort
  62.     printf("After sort:\n");
  63.     for (i=0; i<size; i++)
  64.         printf("%02d:\t%s\n", i, string[i]);
  65.  
  66.  
  67.     }
  68.  
  69. /***********************************************************************8
  70. *Function: GetInput
  71.  
  72.   This function reads character strings and stroes it in a 2D array
  73.  
  74.   *******************************************************************/
  75.  
  76. int GetInput(static char *string, int max_size)
  77.     {
  78.      int i=0;
  79.      int rc;
  80.  
  81.      //Get user input
  82.  
  83.      do{
  84.          rc= scanf("%s", string[i]);
  85.          i++;
  86.  
  87.      }
  88.      while (i<MAX_SIZE && string[i] != EOF);
  89.  
  90.      return(i);
  91. }
Jun 27 '07 #1
1 1540
weaknessforcats
9,208 Expert Mod 8TB
I so confused what to pass into the functions when using 2D arrays.
You pass the name of the array.

The name of the array is address of element 0.

Expand|Select|Wrap|Line Numbers
  1. int array[10];
  2.  
  3. fx(array);
  4.  
  5. //where
  6.  
  7. void fx(int* arg)
  8. {
  9.    //etc...
  10. }
  11.  
Above, the array name is array. That makes it the address of array[0], which is an int. So the name array is an int*.

No problem, right?

This is a little different:
Expand|Select|Wrap|Line Numbers
  1. int array[10][6];
  2.  
  3. fx(array);
  4.  
  5. //where
  6.  
  7. void fx(int (* arg)[6])
  8. {
  9.    //etc...
  10. }
  11.  
Here array has 10 elements. Each element is an array of 6 ints. Therefore, the name array is the address of element 0. Element 0 is an array of 6 int. Therefore, the function argument has to be a pointer to an array of 6 int.

Probably, you need to also pass the number of 6 element arrays. In the example this would be 10.
Jun 27 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
16
by: | last post by:
Hi all, I have a website running on beta 2.0 on server 2003 web sp1 and I keep getting the following error:- Error In:...
3
by: N. Spiker | last post by:
I am attempting to receive a single TCP packet with some text ending with carriage return and line feed characters. When the text is send and the packet has the urgent flag set, the text read from...
2
by: aka | last post by:
Hi all I have two arrays . array1 contains 10 elements array2 contains 6 elements I need to assign the left over 4 elements of array1 into new array . Can anyone give me a peice of code ,how to...
1
by: tigerved | last post by:
Hi I am new to this so I dont exactly know the format in which to post the questions ....anyways here goes ...my file looks like this Year,Day,MOnth,latitude,longitude (commas included)...
1
by: sachin10 | last post by:
hi i m sachin, i m new to VB programming.i face some problem regarding the sorting of text file contents using arrays.text file have data in the format as below seq...
3
by: simpleeshelbee | last post by:
Hey guys, This is my second post and is URGENT!!!! My final assignment is due tonite for class and I have no idea how to write this program right! I am supposed to write a program that uses a...
0
by: fariba123 | last post by:
there are two arrays: $players = array( "23" => "Michael Jordan", "32" => "Michael Johnson" ); $current_player = array( "player_id" => "23", "age" => "22"
7
by: thegreatest21 | last post by:
Right, I am making a Tax Calculator and need an array to store the data that has been typed in when the user is prompted. However, being completely new to Java I am having some difficulty getting to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.