| | |
string problem (IMPORTANT) :((
![]() |
•
•
Join Date: Mar 2006
Posts: 2
Reputation:
Solved Threads: 0
this problem ask the student name and the grades and keep them in string array. but I have to list all of them in the end of program like this:
Number of students to be entered : 4
1. Student name? Jane
1. Student’s grade? 65
2. Student name? Adam
2. Student’s grade? 85
3. Student name? Jash
3. Student’s grade? 75
4. Student name? Mark
4. Student’s grade? 95
Student List in Name Order
===========================
Adam 75
Jane 85
Jash 95
Tom 65
#include <stdio.h>
#include <conio.h>
#include <string.h>
int mystrcmp(char word[], int counter);
int main()
{
int i, n;
char names[100][20] ={ 10 };
int grades[100] = { 0 };
char tmpName[50] = {40};
char *result;
n = 4;
for( i = 0; i < n; i++ )
{
printf( "Student name? " );
tmpName[0] = 40;
result = _cgets(tmpName);
strcpy(names[i],result);
printf( "Student's grade? " );
scanf ( "%d", &grades[i] );
}
mystrcmp(names,n)
for( i = 0; i < n; i++ )
{
printf( "%s\t%d\n", names[i], grades[i] );
}
return 0;
}
int mystrcmp(char word[][20], int counter)
{
char words[counter][20];
int i,c;
for (i=0; i<counter; i++)
{
while (words[i][c] !=NULL && Words[i+1][c] != NULL)
{
if (words[i][c] > words[i+1][c])
{
// I cant complete the function from here.
I want use buble sort but I cant it. someone can help me for this?
Number of students to be entered : 4
1. Student name? Jane
1. Student’s grade? 65
2. Student name? Adam
2. Student’s grade? 85
3. Student name? Jash
3. Student’s grade? 75
4. Student name? Mark
4. Student’s grade? 95
Student List in Name Order
===========================
Adam 75
Jane 85
Jash 95
Tom 65
#include <stdio.h>
#include <conio.h>
#include <string.h>
int mystrcmp(char word[], int counter);
int main()
{
int i, n;
char names[100][20] ={ 10 };
int grades[100] = { 0 };
char tmpName[50] = {40};
char *result;
n = 4;
for( i = 0; i < n; i++ )
{
printf( "Student name? " );
tmpName[0] = 40;
result = _cgets(tmpName);
strcpy(names[i],result);
printf( "Student's grade? " );
scanf ( "%d", &grades[i] );
}
mystrcmp(names,n)
for( i = 0; i < n; i++ )
{
printf( "%s\t%d\n", names[i], grades[i] );
}
return 0;
}
int mystrcmp(char word[][20], int counter)
{
char words[counter][20];
int i,c;
for (i=0; i<counter; i++)
{
while (words[i][c] !=NULL && Words[i+1][c] != NULL)
{
if (words[i][c] > words[i+1][c])
{
// I cant complete the function from here.
I want use buble sort but I cant it. someone can help me for this?
The functions in conio.h are non-standard and not supported by very many compilers. Just a warning because your teacher may not be able to compile your program if he/she uses a compiler that does not support those functions. Most new programmers should not use them so that you learn the generally accepted way to program.
>>char names[100][20] ={ 10 };
what is the significance of 10? why not simply initialize it to 0 like most programmers do?
>>char tmpName[50] = {40};
ditto
>>tmpName[0] = 40;
tmpName is a character array of 40 characters, not an array of pointers. Why are you setting the first element of tempName to 40? What good will that do? _cgets() will just ignore it anyway and fill the buffer with whatever you type, even if you type too many characters. fgets() is a better (and standard) function because it will not overflow the input buffer.
If function mystrcmp() is going to sort the array, then you should name the function that is more descriptive of its true purpose. It contains many uninitialized variables and array. what is the purpose of word array? It isn't initiailized to anything but yet you are attempting to sort it.
There are several variations of the bubble sort, here is the one I use probably because it was the first one I learned. Using the two parameters to the function you posted:
>>char names[100][20] ={ 10 };
what is the significance of 10? why not simply initialize it to 0 like most programmers do?
>>char tmpName[50] = {40};
ditto
>>tmpName[0] = 40;
tmpName is a character array of 40 characters, not an array of pointers. Why are you setting the first element of tempName to 40? What good will that do? _cgets() will just ignore it anyway and fill the buffer with whatever you type, even if you type too many characters. fgets() is a better (and standard) function because it will not overflow the input buffer.
C Syntax (Toggle Plain Text)
fgets(tmpName,sizeof(tmpName), stdin);
If function mystrcmp() is going to sort the array, then you should name the function that is more descriptive of its true purpose. It contains many uninitialized variables and array. what is the purpose of word array? It isn't initiailized to anything but yet you are attempting to sort it.
There are several variations of the bubble sort, here is the one I use probably because it was the first one I learned. Using the two parameters to the function you posted:
C Syntax (Toggle Plain Text)
int i,j; for(i = 0; i < counter-1; i++) { char hold[20]; for(j = (i+1); j < counter; j++) { if( strcmp(words[i],words[j]) < 0) { strcpy(hold,words[i]); strcpy(words[i], words[j]); strcpy(words[j],hold); } } }
![]() |
Similar Threads
- System.String.LastIndexOf problem (C++)
- Newbie string problem (C)
- Is there a simplest way to work this array problem? (C++)
- String Problem (Visual Basic 4 / 5 / 6)
- ANSI string problem (C++)
- String Class Of Java (Java)
Other Threads in the C Forum
- Previous Thread: recursion problem
- Next Thread: Checking for file differences
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork function getlasterror getlogicaldrivestrin givemetehcodez global grade gtkgcurlcompiling gtkwinlinux hacking hardware highest histogram ide include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opendocumentformat opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing segmentationfault sequential single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






