473,395 Members | 1,383 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,395 software developers and data experts.

Finding smallest and largest string

crystal2005
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 12147
JosAH
11,448 Expert 8TB
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
@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 Expert 8TB
@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 512MB
text[i][j]=smallest[0][MAX_CHAR];

What do you try to accomplish here?
Mar 30 '09 #5
@newb16
I want to store the smallest array of string. But the code doesn't seem that way.
Mar 30 '09 #6
newb16
687 512MB
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
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 Expert 8TB
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
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
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...
11
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
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...
19
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...
3
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...
25
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,...
4
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...
9
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.