Click here to Skip to main content
13,597,365 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Intelligent Girl | Introduction to Dynamic Programming 1 & Algorithms Practice Problems | HackerEarth[^]


its a ques from hackerearth. i have submitted successfully. but i have some doubts.

What I have tried:

#include <stdio.h>
int main()
{

    int i,count,j,l;
    char arr[10000];
    scanf("%s",arr);
    
    for(i=0;i<strlen(arr);i++)
    {
            count=0;
    for(j=i;j<strlen(arr);j++)
    {

        if(arr[j]%2==0)
        {

            count++;
        }
    }
        printf("%d ",count);


    }
    return 0;
}

if i will write int arr[10000] instead of char[10000]. it is not working plzz explain me this.
Posted 1-Nov-17 21:34pm
Updated 2-Nov-17 1:27am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

char and int are different sizes, and the system is well aware of this. char variables occupy a single byte, while int values occupy 4 these days - assuming a 32 bit integer, which is normal these days.
When you declare arr as an array of char and use scanf to read a string into it, it reads it fine, and your array indexes fetch a char from each byte - as the index increases by one each time, so does the memory address from which each is fetched.
----- arr points here
|
v
abcdefghijklm
^^^
|||
||--- arr[2]
||
|---- arr[1]
|
----- arr[0]
When you declare it as an int array, the scanf does exactly the same thing, but the indexes increase the memory address by the size of an integer - 4 bytes, rather than one:
----------- arr points here
|
v
abcdefghijklm
^   ^   ^
|   |   |
|   |   --- arr[2]
|   |
|   ------- arr[1]
|
----------- arr[0]
  Permalink  
Comments
jatinp510 2-Nov-17 4:20am
   
got it... Thank you..
OriginalGriff 2-Nov-17 4:30am
   
You're welcome!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

There is a data type mismatch, although mostly it does not prevent you from running the program, only it will give you wrong outputs.

But you need to understand the basic difference in int type and char type, especially in C context. In high-level languages, they sometimes have a same size for data. In C, they have different (a lot different) sizes, char is only 1 byte, and int is 4 bytes (on 32 bit, and different on others). C data types - Wikipedia[^]

Later on, you are trying to do a %d, which expects a decimal. And same is the case for the %s, which expects a char type. So you are printing in one format, and accepting in another, which C will never complain about — only you will. You need to change either %d and work in all characters, or change that %s to work with integers only. Also, once you have made this change, you can select what type of array you want to work with.

Consider reading here, scanf format string - Wikipedia[^]
  Permalink  
v2
Comments
jatinp510 2-Nov-17 4:19am
   
can u give me soln of this ques with int type.
And thanku :)
Afzaal Ahmad Zeeshan 2-Nov-17 4:20am
   
I already have given you the solution, you just need to decide which you want to try out. :-)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Difference: 30000 bytes of memory.


In C programming language, A char is a 1-byte integer while a int is larger (typically 4 bytes).
Strings are array of chars (i.e. functions handling strings, like scanf with %s format specifier, and strlen do expect array of characters as arguments).
You are going to get compiler warnings (and incorrect behaviour) if you use an array of int in your program.
  Permalink  
v2
Comments
jatinp510 2-Nov-17 4:20am
   
thank u..
CPallini 2-Nov-17 4:24am
   
You are welcome.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Not your question, but your program can be made faster.
- Odd/even
Using Modulo to know if a number is odd or even, is more or less doing a division, it cost a lot.
Since the least significant bit is the answer, you can get the answer with a simple bitwise and
- get rid off the nested loops
When you compare 2 consecutive results, they differ only by the odd/even result of first element in sequence.
So once you know the result of full sequence, the second result depend only on the odd/even status of first element, you don't need to recalc the whole sequence.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.180621.3 | Last Updated 2 Nov 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100