how to sort operator | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
- 1

how to sort operator

#include<stdio.h> int main() { char a[3]; int t, tmp; scanf ("%d", &t); for (int i=0; i<=t; i++) { scanf ("%c %c %c", &a[1], &a[2], &a[3]) ;} for(int i=0; i<9; i++)    {        for(int j=i+1; j<5; j++)       {           if(a[i]>a[j])          {              tmp = a[i];             a[i] = a[j];             a[j] = tmp;          }       }    }    for(int i=1; i<t; i++)    {        printf ("%c %c %c", a[i]);    }     } this is my code but there still something wrong... i want the input = 1 (1 is how many time you want input) & ^ ! and the output= ! & ^

13th Oct 2018, 8:42 AM
Yoaraci
Yoaraci - avatar
10 ответов
0
Yoaraci Code that you posted suffer of many problems: 1) Its not compilable probably because there are some strange chars in it 2) You overwrite always "a" items if input give multiple triple of chars 3) You start assigning by index 1 but remember that in C (and almost all languages) index access start from 0 (first item of "a" array is a[0] not a[1]) 4) I dont understand because you putted in sorting loop, 9 and 5... So you will read/write unitialized data 5) space in scanf format string are ignored.. You have to read they also if you want get all data im correct way This code have simple corrections but i didnt make all... Start with understand it and we can go next https://code.sololearn.com/c71987xx31zm/?ref=app
13th Oct 2018, 9:36 AM
KrOW
KrOW - avatar
0
Yoaraci Would be better if you explain what your code do... P.S. Its better save longer codes (more than 7-8 lines) in Code Playground section and link here the url... In this way we can help in more fast way because its more simple to debug
13th Oct 2018, 8:59 AM
KrOW
KrOW - avatar
0
Yoaraci How you have to sort they? Lexically? By precedence? With a custom order?
13th Oct 2018, 9:09 AM
KrOW
KrOW - avatar
0
Maybe im not been clear... When a space is in scanf (and family) format string, then will be readed and ignore all following white space chars (like ' ', '\n', '\t', '\r' etc) while a not-white space is encountered... Read the code, i have updated it with some comments and better handling for your inputs. Beside this you have to handle many other things, the more importants are: 1) what if input count is more than 1? 2) And if its is negative or invalid int? 3) Currently you sort operators lexically but you want order they by precedence
13th Oct 2018, 10:25 AM
KrOW
KrOW - avatar
0
Yoaraci 1) Because you dont know in advance how many inputs you get, you have two choices: read input for input, sort they then print sorted inputs or allocate an array of array chars for contain all inputs (first array, first inputs. second array, second input etc) . For make second one, you have to allocate dynamic memory BUT i suggest to do the first because you have to not allocate nothing. You can use for every input "a" array, sort it, print it and go with next input 2) I assumed that you have to handle invalid input also like: cshhs ^ & ! but maybe its not your case 3) Yes, you are lucky but if you have to compare others operators you would be less lucky 😁 Dont worry, none born yet learned 😉
13th Oct 2018, 11:14 AM
KrOW
KrOW - avatar
- 1
https://code.sololearn.com/c71lFfIS5TX7/?ref=app actually i want to sort three operator that have to be inputted in one line and each operator have to be seperated with a space for example: input =& ^ ! output= ! & ^ but i think there are still so many things that wrong in my codes TT
13th Oct 2018, 9:08 AM
Yoaraci
Yoaraci - avatar
- 1
i want to sort them by precedence
13th Oct 2018, 9:10 AM
Yoaraci
Yoaraci - avatar
- 1
oh i forgot to change the number in sorting loop because i copy it from other code haha... thanks for the answer :) but about the scanf, in my assignment i have to input with the spaces. so i have to input $ ^ &, the one who check my assignment dont want accept it if the input $^&
13th Oct 2018, 9:48 AM
Yoaraci
Yoaraci - avatar
- 1
1. yes actually i want the input more than one but i dont know how to make the code read all the input in the other statement 2. sorry what do you mean invalid int? 3. oh i think i already sort it by precedence because when i test it, i already have the same output as the example in my assignment haha... sorry because there are many things that i don't know because i'm still new in coding haha
13th Oct 2018, 10:47 AM
Yoaraci
Yoaraci - avatar
- 1
1. then i choose the first that seem easier than the first one 😆😆 2. oh i don't think i have to input the other except for the operator. so i think its okay as long i can use operator 3. hahaha maybe i am indeed lucky at that time. the assignment said that i have to used precendence, so maybe i have to change the code. how to make it sort by precedence?
13th Oct 2018, 11:28 AM
Yoaraci
Yoaraci - avatar