• Skip to main content
  • Skip to footer

SoftwareProg.com

Software programming learning aide

  • Blog
    • Discussion
  • News
  • CS50’s Introduction to Computer Science by Harvard University on Edx
    • Week 1
    • Week 2
    • Week 3
    • Week 4
    • Week 5

How to form a new array with ASCII characters as input

CS50 threads to aide as a supplementary resource › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2 › How to form a new array with ASCII characters as input

Tagged: arrays, Caesar project

  • This topic is empty.
Log In Register Lost Password
Viewing 1 post (of 1 total)
  • Author
    Posts
  • January 21, 2022 at 10:35 am #157
    admin
    Keymaster

      Suppose I need to create a new array of 26 elements that will be derived from ASCII value of 26 uppercase characters (65 – 90). Instead of taking input from an user individually like in the below code, the code should perhaps use arrays that will not need a user to input characters one by one.


      Copy Code
      Copied
      Use a different Browser

      
      
      #include<cs50.h>
      #include<stdio.h>
      #include<string.h>
      #include<ctype.h>
      int main(void)
      {
          char userinput;
          userinput = get_char("enter character");
          printf("ASCII of %c is %i", userinput, userinput);
      }
      

      Array declaration will be:

      char uppercase_array[25];

      This is the way when elements A, B, C… are entered manually as value. Definitely, not the desired way.

       


      Copy Code
      Copied
      Use a different Browser

      
      
      {
          char name_array[] = {'A','B'};
          printf("second ascii value is %i", name_array[1]);
      }

      Struggling to proceed and seeking help.There is perhaps a need to create a for loop.

      Revised code:


      Copy Code
      Copied
      Use a different Browser

      
      
      #include<stdio.h>
      #include<cs50.h>
      #include<ctype.h>
      #include<string.h>
      int main(void)
      {
          char uppercase_array;
          char newuppercase_array;
          for (uppercase_array = 65; uppercase_array <= 90; uppercase_array i++)//will start with first element of uppercase_array upto 90
          {
          newuppercase_array = uppercase_array - 65;//the proposed newuppercase-array will be filled with the 25 elements of uppercase-array sequentially
          }
      }

      The above code is showing this error:


      Reply


      Well first of all, if you want your variables to be an array you should declare them as an array:


      Copy Code
      Copied
      Use a different Browser

      
      
      char newuppercase_array; //wrong
      char newuppercase_array[26]; //right

      that will be derived from ASCII value of 26 uppercase characters (65 – 90)

      The simplest way to do this from the computer’s point of view is to just… do it manually. You know the 26 values you want, so store them in the array:


      Copy Code
      Copied
      Use a different Browser

      
      
      char newuppercase_array[] = {65, 66, 67, 68, ...}

      Or even use the characters themselves to be clearer:


      Copy Code
      Copied
      Use a different Browser

      
      
      char newuppercase_array[] = {'A', 'B', 'C', 'D', ...}

       

      But if you wanted to save yourself from writing that, yes, you could use a loop:


      Copy Code
      Copied
      Use a different Browser

      
      
      for (int i = 0; i < 26; i++) {
          newuppercase_array = i + 'A';
      }

       


      I tried with this code and getting segmentation fault. Although you have provided for loop with i = 0, I am trying with this out of curiosity:

       


      Copy Code
      Copied
      Use a different Browser

      
      
      #include<cs50.h>
      #include<stdio.h>
      #include<ctype.h>
      #include<string.h>
      
      int main(void)
      
      {
      char uppercase_array[25];
      char newuppercase_array[25];
      for (int i = 65; i <= 90; i++)//will start with first element of uppercase_array upto 90
      {
      newuppercase_array = uppercase_array - 65;//the proposed newuppercase-array will be filled with the 25 elements of uppercase-array sequentially
      }
      
      }

       

      While the program compiles, as I enter ./NameofFile, I get: Segmentation fault (core dumped).


      Reply


      Well there are two big issues there. First, you started i at 65, and then tried to use it as an index for your array. Your arrays don’t have 66 elements, so you can’t use 65 as an index for them.

      Second:


      Copy Code
      Copied
      Use a different Browser

      
      
      newuppercase_array = uppercase_array -65

      What value do you expect this to have when you haven’t initialized uppercase_array?


      Reply



      Copy Code
      Copied
      Use a different Browser

      
      
      #include<stdio.h>
      #include<cs50.h>
      #include<ctype.h>
      #include<string.h>
      int main(void)
      {
      char newuppercase_array[26];
          for (int i = 0; i < 26; i++)
          {
          newuppercase_array = i + 'A';
          printf("%i,",newuppercase_array);
          }
      }

      While the above code compiling and giving output in terms of ASCII , how do I get the output in character form? For instance, A instead of 65

      cs50

      I tried with the below, replacing %i by %c, but did not work:


      Copy Code
      Copied
      Use a different Browser

      
      
      {
      
      char newuppercase_array[26];
          for (int i = 0; i < 26; i++)
          {
          newuppercase_array = i + 'A';
          printf("%c,",newuppercase_array[c]);
          }
      }


      Reply


      printf(“%c,”,newuppercase_array[c]);

      You don’t have a variable called c, so you can’t use that as your array index.


      How do I then print characters (A,B,C,…Z) instead of ASCII codes (65,66,67…90).


      Reply


      You use the %c format specifier. This is completely separate from the index of your array.


      Reply on Edx


      https://edstem.org/us/courses/176/discussion/1019821?comment=2332440[learn_press_profile]

    • Author
      Posts
    Log In Register Lost Password
    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.
    Log In

    Log in / Register

    Initiative by Digital Splendid OPC Pvt. Ltd.