how many times a number, given by user from keyboard, is occured in the array












-1















The program should calculate: How many times a number, given by user from keyboard, occurs in the array. I have seen similar examples but in this case it is not about the frequency of numbers of the given array, it's about same numbers between users numbers and the given array. So I couldn't make it.



How can we do this just using arrays, if-else condition and for loop?



#include <stdio.h>

int main()
{
int N=6, size;
int a = {1,2,3,4,5,6};
int occ [size];
int i,j;
int number;
int occured = 0;

printf ("enter the size of arrayn");
scanf ("%d", &size);

printf("Enter elements in array:n");
for (j=0; j<size; ++j){
scanf ("%d", &occ[j]);
}

for (j=0; j<size; ++j) {
for (i=0; i<N; ++i) {
if (occ[j] == a[i])
occured = occured + 1;
}
}

printf ("given numbers occured in array %d times.", occured);
return 0;
}









share|improve this question

























  • size is not initialized when you use it to declare int occ[size], so how big is the occ array? There are other things wrong but always remember to initialize your variables before you use them. Its easy to just always initialize them (to zero) when you declare them.

    – Bwebb
    Nov 20 '18 at 0:05











  • okay i will keep in mind. i know that all code is totally wrong but it was just an idea. i couldn`t find the algorithm for the problem

    – noob
    Nov 20 '18 at 0:11











  • if you have the user input you only need to compare that to each element in the array, you should not need the nested for loop. You are currently counting how many times 1 or 2 or 3 or 4 or 5 or 6 are in the array. From the question it sounds like you just to count how many times X is in the array, for some value of X inputted by the user.

    – Bwebb
    Nov 20 '18 at 0:17











  • If you compiled with -Wall, it would flag int occ [size]; because at the point where you've placed it, size has yet to be given a value. Move it after the scanf("%d", &size); line. That is, do this: scanf("%d", &size); int occ[size];

    – Craig Estey
    Nov 20 '18 at 0:20













  • can you give a sample or expected input-output pair @noob?

    – Rai
    Nov 20 '18 at 6:29
















-1















The program should calculate: How many times a number, given by user from keyboard, occurs in the array. I have seen similar examples but in this case it is not about the frequency of numbers of the given array, it's about same numbers between users numbers and the given array. So I couldn't make it.



How can we do this just using arrays, if-else condition and for loop?



#include <stdio.h>

int main()
{
int N=6, size;
int a = {1,2,3,4,5,6};
int occ [size];
int i,j;
int number;
int occured = 0;

printf ("enter the size of arrayn");
scanf ("%d", &size);

printf("Enter elements in array:n");
for (j=0; j<size; ++j){
scanf ("%d", &occ[j]);
}

for (j=0; j<size; ++j) {
for (i=0; i<N; ++i) {
if (occ[j] == a[i])
occured = occured + 1;
}
}

printf ("given numbers occured in array %d times.", occured);
return 0;
}









share|improve this question

























  • size is not initialized when you use it to declare int occ[size], so how big is the occ array? There are other things wrong but always remember to initialize your variables before you use them. Its easy to just always initialize them (to zero) when you declare them.

    – Bwebb
    Nov 20 '18 at 0:05











  • okay i will keep in mind. i know that all code is totally wrong but it was just an idea. i couldn`t find the algorithm for the problem

    – noob
    Nov 20 '18 at 0:11











  • if you have the user input you only need to compare that to each element in the array, you should not need the nested for loop. You are currently counting how many times 1 or 2 or 3 or 4 or 5 or 6 are in the array. From the question it sounds like you just to count how many times X is in the array, for some value of X inputted by the user.

    – Bwebb
    Nov 20 '18 at 0:17











  • If you compiled with -Wall, it would flag int occ [size]; because at the point where you've placed it, size has yet to be given a value. Move it after the scanf("%d", &size); line. That is, do this: scanf("%d", &size); int occ[size];

    – Craig Estey
    Nov 20 '18 at 0:20













  • can you give a sample or expected input-output pair @noob?

    – Rai
    Nov 20 '18 at 6:29














-1












-1








-1








The program should calculate: How many times a number, given by user from keyboard, occurs in the array. I have seen similar examples but in this case it is not about the frequency of numbers of the given array, it's about same numbers between users numbers and the given array. So I couldn't make it.



How can we do this just using arrays, if-else condition and for loop?



#include <stdio.h>

int main()
{
int N=6, size;
int a = {1,2,3,4,5,6};
int occ [size];
int i,j;
int number;
int occured = 0;

printf ("enter the size of arrayn");
scanf ("%d", &size);

printf("Enter elements in array:n");
for (j=0; j<size; ++j){
scanf ("%d", &occ[j]);
}

for (j=0; j<size; ++j) {
for (i=0; i<N; ++i) {
if (occ[j] == a[i])
occured = occured + 1;
}
}

printf ("given numbers occured in array %d times.", occured);
return 0;
}









share|improve this question
















The program should calculate: How many times a number, given by user from keyboard, occurs in the array. I have seen similar examples but in this case it is not about the frequency of numbers of the given array, it's about same numbers between users numbers and the given array. So I couldn't make it.



How can we do this just using arrays, if-else condition and for loop?



#include <stdio.h>

int main()
{
int N=6, size;
int a = {1,2,3,4,5,6};
int occ [size];
int i,j;
int number;
int occured = 0;

printf ("enter the size of arrayn");
scanf ("%d", &size);

printf("Enter elements in array:n");
for (j=0; j<size; ++j){
scanf ("%d", &occ[j]);
}

for (j=0; j<size; ++j) {
for (i=0; i<N; ++i) {
if (occ[j] == a[i])
occured = occured + 1;
}
}

printf ("given numbers occured in array %d times.", occured);
return 0;
}






c arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 7:08









Rai

820418




820418










asked Nov 19 '18 at 23:53









noobnoob

82




82













  • size is not initialized when you use it to declare int occ[size], so how big is the occ array? There are other things wrong but always remember to initialize your variables before you use them. Its easy to just always initialize them (to zero) when you declare them.

    – Bwebb
    Nov 20 '18 at 0:05











  • okay i will keep in mind. i know that all code is totally wrong but it was just an idea. i couldn`t find the algorithm for the problem

    – noob
    Nov 20 '18 at 0:11











  • if you have the user input you only need to compare that to each element in the array, you should not need the nested for loop. You are currently counting how many times 1 or 2 or 3 or 4 or 5 or 6 are in the array. From the question it sounds like you just to count how many times X is in the array, for some value of X inputted by the user.

    – Bwebb
    Nov 20 '18 at 0:17











  • If you compiled with -Wall, it would flag int occ [size]; because at the point where you've placed it, size has yet to be given a value. Move it after the scanf("%d", &size); line. That is, do this: scanf("%d", &size); int occ[size];

    – Craig Estey
    Nov 20 '18 at 0:20













  • can you give a sample or expected input-output pair @noob?

    – Rai
    Nov 20 '18 at 6:29



















  • size is not initialized when you use it to declare int occ[size], so how big is the occ array? There are other things wrong but always remember to initialize your variables before you use them. Its easy to just always initialize them (to zero) when you declare them.

    – Bwebb
    Nov 20 '18 at 0:05











  • okay i will keep in mind. i know that all code is totally wrong but it was just an idea. i couldn`t find the algorithm for the problem

    – noob
    Nov 20 '18 at 0:11











  • if you have the user input you only need to compare that to each element in the array, you should not need the nested for loop. You are currently counting how many times 1 or 2 or 3 or 4 or 5 or 6 are in the array. From the question it sounds like you just to count how many times X is in the array, for some value of X inputted by the user.

    – Bwebb
    Nov 20 '18 at 0:17











  • If you compiled with -Wall, it would flag int occ [size]; because at the point where you've placed it, size has yet to be given a value. Move it after the scanf("%d", &size); line. That is, do this: scanf("%d", &size); int occ[size];

    – Craig Estey
    Nov 20 '18 at 0:20













  • can you give a sample or expected input-output pair @noob?

    – Rai
    Nov 20 '18 at 6:29

















size is not initialized when you use it to declare int occ[size], so how big is the occ array? There are other things wrong but always remember to initialize your variables before you use them. Its easy to just always initialize them (to zero) when you declare them.

– Bwebb
Nov 20 '18 at 0:05





size is not initialized when you use it to declare int occ[size], so how big is the occ array? There are other things wrong but always remember to initialize your variables before you use them. Its easy to just always initialize them (to zero) when you declare them.

– Bwebb
Nov 20 '18 at 0:05













okay i will keep in mind. i know that all code is totally wrong but it was just an idea. i couldn`t find the algorithm for the problem

– noob
Nov 20 '18 at 0:11





okay i will keep in mind. i know that all code is totally wrong but it was just an idea. i couldn`t find the algorithm for the problem

– noob
Nov 20 '18 at 0:11













if you have the user input you only need to compare that to each element in the array, you should not need the nested for loop. You are currently counting how many times 1 or 2 or 3 or 4 or 5 or 6 are in the array. From the question it sounds like you just to count how many times X is in the array, for some value of X inputted by the user.

– Bwebb
Nov 20 '18 at 0:17





if you have the user input you only need to compare that to each element in the array, you should not need the nested for loop. You are currently counting how many times 1 or 2 or 3 or 4 or 5 or 6 are in the array. From the question it sounds like you just to count how many times X is in the array, for some value of X inputted by the user.

– Bwebb
Nov 20 '18 at 0:17













If you compiled with -Wall, it would flag int occ [size]; because at the point where you've placed it, size has yet to be given a value. Move it after the scanf("%d", &size); line. That is, do this: scanf("%d", &size); int occ[size];

– Craig Estey
Nov 20 '18 at 0:20







If you compiled with -Wall, it would flag int occ [size]; because at the point where you've placed it, size has yet to be given a value. Move it after the scanf("%d", &size); line. That is, do this: scanf("%d", &size); int occ[size];

– Craig Estey
Nov 20 '18 at 0:20















can you give a sample or expected input-output pair @noob?

– Rai
Nov 20 '18 at 6:29





can you give a sample or expected input-output pair @noob?

– Rai
Nov 20 '18 at 6:29












2 Answers
2






active

oldest

votes


















0














This will solve you problem.



#include <stdio.h>

int main()
{
int N = 6, size;
int a = {1,2,3,4,5,6};
int i, j;
int occured = 0;

printf ("enter the size of arrayn");
scanf ("%d", &size);

int occ [size];
printf("Enter elements in array:n");
for (j=0; j<size; ++j){
scanf ("%d", &occ[j]);
}

for (j=0; j<size; ++j) {
for (i=0; i<N; ++i) {
if (occ[j] == a[i])
occured = occured + 1;
}
}

printf ("given numbers occured in array %d times.", occured);
return 0;
}





share|improve this answer































    0














    Your code has some mistakes which have been cleared by @Bwebb so I won't come into that.
    Coming back to your problem, this can easily be solved by storing frequency using c++ map container. But since you are trying to implement it using only arrays there is an another way around.



    You can make an another array with a fairly large size and initialise all it's elements to 0. Then you take the input from the user for the array and change into it's ASCII value if it's an alphabet when you put it in the index. For everytime the same input comes, you will increase the value of the corresponding index by 1, like this:a[1]++. And whenever you have to find the frequency of a character, you just have to put it in the index of a and print the output.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384318%2fhow-many-times-a-number-given-by-user-from-keyboard-is-occured-in-the-array%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      This will solve you problem.



      #include <stdio.h>

      int main()
      {
      int N = 6, size;
      int a = {1,2,3,4,5,6};
      int i, j;
      int occured = 0;

      printf ("enter the size of arrayn");
      scanf ("%d", &size);

      int occ [size];
      printf("Enter elements in array:n");
      for (j=0; j<size; ++j){
      scanf ("%d", &occ[j]);
      }

      for (j=0; j<size; ++j) {
      for (i=0; i<N; ++i) {
      if (occ[j] == a[i])
      occured = occured + 1;
      }
      }

      printf ("given numbers occured in array %d times.", occured);
      return 0;
      }





      share|improve this answer




























        0














        This will solve you problem.



        #include <stdio.h>

        int main()
        {
        int N = 6, size;
        int a = {1,2,3,4,5,6};
        int i, j;
        int occured = 0;

        printf ("enter the size of arrayn");
        scanf ("%d", &size);

        int occ [size];
        printf("Enter elements in array:n");
        for (j=0; j<size; ++j){
        scanf ("%d", &occ[j]);
        }

        for (j=0; j<size; ++j) {
        for (i=0; i<N; ++i) {
        if (occ[j] == a[i])
        occured = occured + 1;
        }
        }

        printf ("given numbers occured in array %d times.", occured);
        return 0;
        }





        share|improve this answer


























          0












          0








          0







          This will solve you problem.



          #include <stdio.h>

          int main()
          {
          int N = 6, size;
          int a = {1,2,3,4,5,6};
          int i, j;
          int occured = 0;

          printf ("enter the size of arrayn");
          scanf ("%d", &size);

          int occ [size];
          printf("Enter elements in array:n");
          for (j=0; j<size; ++j){
          scanf ("%d", &occ[j]);
          }

          for (j=0; j<size; ++j) {
          for (i=0; i<N; ++i) {
          if (occ[j] == a[i])
          occured = occured + 1;
          }
          }

          printf ("given numbers occured in array %d times.", occured);
          return 0;
          }





          share|improve this answer













          This will solve you problem.



          #include <stdio.h>

          int main()
          {
          int N = 6, size;
          int a = {1,2,3,4,5,6};
          int i, j;
          int occured = 0;

          printf ("enter the size of arrayn");
          scanf ("%d", &size);

          int occ [size];
          printf("Enter elements in array:n");
          for (j=0; j<size; ++j){
          scanf ("%d", &occ[j]);
          }

          for (j=0; j<size; ++j) {
          for (i=0; i<N; ++i) {
          if (occ[j] == a[i])
          occured = occured + 1;
          }
          }

          printf ("given numbers occured in array %d times.", occured);
          return 0;
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 7:19









          ChameeraChameera

          628




          628

























              0














              Your code has some mistakes which have been cleared by @Bwebb so I won't come into that.
              Coming back to your problem, this can easily be solved by storing frequency using c++ map container. But since you are trying to implement it using only arrays there is an another way around.



              You can make an another array with a fairly large size and initialise all it's elements to 0. Then you take the input from the user for the array and change into it's ASCII value if it's an alphabet when you put it in the index. For everytime the same input comes, you will increase the value of the corresponding index by 1, like this:a[1]++. And whenever you have to find the frequency of a character, you just have to put it in the index of a and print the output.






              share|improve this answer




























                0














                Your code has some mistakes which have been cleared by @Bwebb so I won't come into that.
                Coming back to your problem, this can easily be solved by storing frequency using c++ map container. But since you are trying to implement it using only arrays there is an another way around.



                You can make an another array with a fairly large size and initialise all it's elements to 0. Then you take the input from the user for the array and change into it's ASCII value if it's an alphabet when you put it in the index. For everytime the same input comes, you will increase the value of the corresponding index by 1, like this:a[1]++. And whenever you have to find the frequency of a character, you just have to put it in the index of a and print the output.






                share|improve this answer


























                  0












                  0








                  0







                  Your code has some mistakes which have been cleared by @Bwebb so I won't come into that.
                  Coming back to your problem, this can easily be solved by storing frequency using c++ map container. But since you are trying to implement it using only arrays there is an another way around.



                  You can make an another array with a fairly large size and initialise all it's elements to 0. Then you take the input from the user for the array and change into it's ASCII value if it's an alphabet when you put it in the index. For everytime the same input comes, you will increase the value of the corresponding index by 1, like this:a[1]++. And whenever you have to find the frequency of a character, you just have to put it in the index of a and print the output.






                  share|improve this answer













                  Your code has some mistakes which have been cleared by @Bwebb so I won't come into that.
                  Coming back to your problem, this can easily be solved by storing frequency using c++ map container. But since you are trying to implement it using only arrays there is an another way around.



                  You can make an another array with a fairly large size and initialise all it's elements to 0. Then you take the input from the user for the array and change into it's ASCII value if it's an alphabet when you put it in the index. For everytime the same input comes, you will increase the value of the corresponding index by 1, like this:a[1]++. And whenever you have to find the frequency of a character, you just have to put it in the index of a and print the output.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 0:21









                  KrabKrab

                  11




                  11






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384318%2fhow-many-times-a-number-given-by-user-from-keyboard-is-occured-in-the-array%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith