› Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2: [Arrays] – Functions, Variable and Scope, Debugging, Arrays, and Command Line Arguments › Seeking help in expressing to code a known element within argv[1][ ]
- This topic is empty.
- 
		AuthorPosts
- 
		
			
				
February 22, 2022 at 6:11 am #238In case of HELLO as plaintext, int s[0] – 65 == argv[1][7] A problem faced is how to express int s[0] – 65 = 7 within argv[1][ ]. 
 Reply 
 Why do you want to check if ‘H’ – 65 has same value as the 8th element of the key? ‘H’ – 65 = 72 – 65 = 7 So on the left side you have found the position of ‘H’ in the alphabet and that is great! Use that position to find the substitute letter, not to compare with it 🙂 
 Query 
 int s[0] – 65 == argv[1][7] It is clear that s[0] will have the value of argv[1][7]. I doubt the below the right way: int s[0] – 65 = argv[1][7] as how can I put 7 inside argv[1][7]. I think I need to find a way to put 7 in second square bracket in a way that captures input from int s[0] – 65 instead of directly putting 7. Any clue appreciated. 
 Reply 
 I think I need to find a way to put 7 in second square bracket in a way that captures input from int s[0] – 65 There you kind of have it yourself ….. just forget about the number 7!!! Use what leads you to 7, “s[0] – 65″!! You don’t need to find a way, just do it: [dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”] argv[1][s[0] - 65] [/dm_code_snippet] There! No special way, just put your expression inside the brackets 🙂 Then make your loop to get all the letters of your plaintext, not only s[0]. Almost there 🙂 
 Query 
 argv[1][s[0] – 65] Could you please explain why argv[1][int s[0] – 65] not used. My understanding is that since we are doing arithmetical calculation, int s[0] will fetch 72 and s[0] will fetch H. The reason could be as the end output (8th key) will be fetched in the alphabet and not its index. If so, then s[0] – 65 is able to perform arithmetical calculation? I actually tried with s[i] = argv[1][int s[i] – 65]; and it is showing error.  
 Reply 
 When you use “int” in that line it looks to C like you are trying to declare a new int variable and that does not make sense. To get the understanding of char and int in the context of letters try to run this short program: [dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”] #include int main(void) { char letters[4] = {'A', 'B', 'C', 'D'}; printf("Letter as char: %c\n", letters[0]); printf("Letter as int: %i\n", letters[0]); printf("Letter as int in formula: %i\n", letters[0] - 65); }[/dm_code_snippet] You will see that ‘A’ can be used both as a number and as a character and can be used directly in a formula 🙂 What you are trying to do with “int s[0]” is to “cast” s[0] as an integer. The syntax for doing that in C is “(int) s[0]” …. but that is not needed here. 
 Reply 
 On Edx forum, one expert advised to use a variable (https://edstem.org/us/courses/176/discussion/1164829?comment=2689537). I have come up with this code: int s[0] – 65 = t s[0] = argv[1][t] Is the above correct? [learn_press_profile] 
- 
		AuthorPosts
- You must be logged in to reply to this topic.


