473,748 Members | 10,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding smallest and largest string

crystal2005
44 New Member
I am writing a program that receive maximum of 25 line of string each has 20 characters maximum. The program will print the smallest and the largest string. However the following program gives me Segmentation fault (core dumped) :(( It looks simple but i have no idea what went wrong....

Can anyone help me out??

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. #define MAX_INPUT 25
  6. #define MAX_CHAR 20
  7.  
  8. int main(void)
  9. {
  10.     int t, i, j;
  11.  
  12.     char text[MAX_INPUT][MAX_CHAR];
  13.  
  14.     char smallest[0][MAX_CHAR], largest[0][MAX_CHAR];
  15.  
  16.     printf("Enter 4 characters to quit. \n");
  17.  
  18.     for(t=0; t<MAX_INPUT; t++)
  19.     {
  20.         printf("Enter word %d: ", t);
  21.         gets(text[t]);
  22.         if(strlen(text[t])==4) break;
  23.     }
  24.  
  25.  
  26.     for(i=0; i<t; i++)
  27.     {
  28.         for(j=0; text[i][j]; j++)
  29.         {
  30.                 if(text[i][j]<smallest[0][MAX_CHAR])
  31.                 {
  32.                     text[i][j]=smallest[0][MAX_CHAR];
  33.                 }
  34.                 if(text[i][j]>largest[0][MAX_CHAR])
  35.                 {
  36.                     text[i][j]=largest[0][MAX_CHAR];
  37.                 }
  38.         }
  39.     }
  40.  
  41.     printf("Smallest word: %s\n", smallest[0][MAX_CHAR]);
  42.     printf("Largest word: %s\n", largest[0][MAX_CHAR]);
  43.  
  44.     return EXIT_SUCCESS;
  45.  
Mar 30 '09 #1
8 12181
JosAH
11,448 Recognized Expert MVP
You don't have to remember all those strings; all you have to remember are the smallest and largest strings so far. After having read the first string that string is the smalles and largest string so far.

kind regards,

Jos
Mar 30 '09 #2
crystal2005
44 New Member
@JosAH
Sorry Jos, what do you mean the first string is the smallest and largest string so far??
Mar 30 '09 #3
JosAH
11,448 Recognized Expert MVP
@crystal2005
Suppose you haven't read any strings yet; you don't know what the smallest or largest strings so far are; suppose you have read one string; it has to be the smallest and largest string read so far.

kind regards,

Jos
Mar 30 '09 #4
newb16
687 Contributor
text[i][j]=smallest[0][MAX_CHAR];

What do you try to accomplish here?
Mar 30 '09 #5
crystal2005
44 New Member
@newb16
I want to store the smallest array of string. But the code doesn't seem that way.
Mar 30 '09 #6
newb16
687 Contributor
char largest[0][MAX_CHAR] is a wrong way to store string - sizeof(largest) on my machine returns 0, because it's an array of zero arrays of N chars. However largest[0][MAX_CHAR] is located at 25 bytes from its beginning, overlapping some other variable. Also, you never assign anything to 'smallest' but print it after all.
Mar 30 '09 #7
crystal2005
44 New Member
After trying for quite sometime, it does work... but my code only take one character comparison in front. Any suggestion to compare probably up to 2 or 3 characters for each string??

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. #define MAX_INPUT 25
  6. #define MAX_CHAR 20
  7.  
  8. int main(void)
  9. {
  10.     int t, i;
  11.  
  12.     char text[MAX_INPUT][MAX_CHAR];    
  13.  
  14.     char* largestString;
  15.     char* smallestString;
  16.     unsigned int largestStringASCII;
  17.     unsigned int smallestStringASCII;
  18.  
  19.     unsigned int largestValue = 0;
  20.     unsigned int smallestValue = 127;    //ASCII max No.
  21.  
  22.     printf("Enter 4 characters to quit. \n");
  23.  
  24.     for(t=0; t<MAX_INPUT; t++)
  25.     {
  26.         printf("Enter word %d: ", t);
  27.         gets(text[t]);
  28.         if(strlen(text[t])==4) break;
  29.     }
  30.  
  31.  
  32.     for(i=0; i<=t; i++)
  33.     {        
  34.         largestStringASCII = text[i][0];
  35.         if(largestStringASCII>largestValue)
  36.         {
  37.             largestValue = largestStringASCII;
  38.             largestString = text[i];
  39.         }
  40.     }
  41.  
  42.     for(i=0; i<=t; i++)
  43.     {
  44.         smallestStringASCII = text[i][0];
  45.         if(smallestStringASCII<smallestValue)
  46.         {
  47.             smallestValue = smallestStringASCII;
  48.             smallestString = text[i];
  49.         }
  50.     }
  51.  
  52.     printf("Smallest word: %s\n", smallestString);
  53.     printf("Largest word: %s\n", largestString);
  54.  
  55.     return EXIT_SUCCESS;
  56.  
Mar 30 '09 #8
JosAH
11,448 Recognized Expert MVP
Why don't you use the strcmp( ... ) function? It was made for these purposes.

kind regards,

Jos
Mar 30 '09 #9

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

Similar topics

2
8891
by: Keke922 | last post by:
I have to write a program that allows the user to enter a series of integers and -99 when they want to exit the loop. How do I display the largest and smallest number the user entered?
4
6485
by: Code4u | last post by:
I need to write an algorithm that sheds the outliers in a large data set, for example, I might want to ignore the smallest 2% of values and find the next smallest. Boost has a nth_element algorithm, however, it partially sorts the data. I have a requirement that the data remain in the orginal order. Of course I could make a copy of the vector or array and then apply nth_element, but this would be expensive in memory and would require...
11
2383
by: dmsy | last post by:
Is there a way to use javascript to increase the overall document text size, just like if you click View->Text Size->Largest, Larger, Medium, Smaller, Smallest? This is on IE browser.
13
5151
by: Peter Ammon | last post by:
I have a floating point number. I'd like to get the nearest floating point number that is larger or smaller than the given number. I investigated FLT_EPSILON but it only seems to be useful if the given number is 1. Any suggestions? Thanks, -Peter
19
8588
by: ramu | last post by:
Hi, I have, suppose 1000 numbers, in a file. I have to find out 5 largest numbers among them without sorting. Can you please give me an efficient idea to do this? My idea is to put those numbers into a binary tree and to find the largest numbers. How else can we do it? Regards
3
3949
by: HEMH6 | last post by:
Who can help solve this problem??? Finding the Largest Value (a) write a function, largest(), that returns the largest value in a signed integer array. The array and its size are passed as arguments. (b)Write a main program that inputs MAX values from the keyboard into a signed integer array, array, and points, using largest(), the largest value to the screen.
25
4368
by: Subra | last post by:
Hi, What is the best way to find the 1000 largest numbers from the file having hell lot of entries ? Can you please help me to find out the way ? Do I need to go for B+ trees ?? Please help, Subramanya M
4
47714
by: MBeckford05 | last post by:
Hello, I am trying to write java program to input three integer numbers compare them and display the largest and Smallest number. I have thought about the problem I would need to use a comparison operate determine which number is larger and which is smaller. I am new to java I was wondering how to but this into java source code. Can anyone Help? Thank You. MBeckfod05
9
3060
by: tom | last post by:
Hi! How can I determine the smallest and largest values of numeric types (for example int) possible in my system? I think there exists a function for this task but I don't know it.
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.