Jump to content

Im almost there guys! need ur help

- - - - -

  • Please log in to reply
6 replies to this topic

#1
SpaceCowboy

SpaceCowboy

    Newbie

  • Members
  • Pip
  • 8 posts
So as i mentioned i am rly bad at C, just started and i made a program, i think in a worst way it can be done but gives me the right answers, but can anyone write me a command line showing how many digits in sum are equal to 3
Here is my program dont laugh since this is the best one i've made so far. :w00t:


#include <stdio.h>
#include <stdlib.h>

int main ()
{
int num, suma, a1, a2, a3, a4, a5, a6, max, I, n;
printf("Enter any number: \n");
scanf("%d", &num);
printf("Using a formula Sum=1+N+N*N (N=number of your choice)\n");
suma=1+num+num*num;
printf ("Sum: %d \n", suma);
a1=suma%10;
a2=suma/10%10;
a3=suma/100%10;
a4=suma/1000%10;
a5=suma/10000%10;
a6=suma/100000%10;
{
max=0;
for(I=0;I<n;I++);
{
if(max<a1)
{
max=a1;
}
if(max<a2)
{
max=a2;
}
if(max<a3)
{
max=a3;
}
if(max<a4)
{
max=a4;
}
if(max<a5)
{
max=a5;
}
if(max<a6)
{
max=a6;
}
}
printf("max=%d \n",max);
}
}

Edited by WingedPanther, 01 December 2010 - 06:39 PM.
add code tags (the # button)


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
If you mean adapting your code to find all '3's in a sum given, you would need to modify your code to run in loops. For example
1) Get length of integer so you know how many times to extract each number
2) Run loop X times the length of the number
3) Loop to compare X results against '3'.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
SpaceCowboy

SpaceCowboy

    Newbie

  • Members
  • Pip
  • 8 posts
Yeah thats what i want to get, I'll try, thou i have never even read about loop ;o

#4
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

You have to loop to determine the length of the number as 'n'; and then again using a loop upto 'n' times to get maximum digit!

#5
SpaceCowboy

SpaceCowboy

    Newbie

  • Members
  • Pip
  • 8 posts
but how do i set the lengh of sum, i can set it if i choose any number but sum lengh is not up to me ;o

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You can use the same formula to grab the length of an integer through division, here is an example:
int intlen(int integer) {
    int i = 1;
    while ( (integer /= 10) > 0)
        i++;
    return i;
}
If you happened to go over the limit of the formula (such as integer /= 100000000) it will return a negative result, so we can safely break the loop and return the true count of numbers within an integer.

You can then of course store the individual lengths in an array instead of a1, a2, a3 .... a6 to make the program more flexible.
    //get length of sum
    length = intlen(suma);

    //prepare array to store results, a dynamic array of 'length' size
    int results[length]; 
    int divisor = 1;
    
    //store results in array
    for(i = 0; i <= length - 1; i++) {
        results[i] = suma / divisor % 10;
        divisor *= 10;
    }
notice how I set the divisor out of the loop, and increase it by 10 each time the loop happens so 10 -> 100 -> 1000 -> 10000 just like in your original equation, we then add it to the array dynamically using i as an index.

You can now iterate through the loop, here we will just print it, but you can check for 3's and create a count on each occurrence without much trouble:
    //now loop through array and print
    for(i = 0; i <= length - 1; i++) {
            printf("index %d = %d\n", i, results[i]);
    }
That code will work for any length number and result, as long as it is below integer max (~2.7 billion on 32 bit platforms, so you may wish to add checks for that if you wish)

A few more things you may wish to notice, such as array indexes starting at zero, so to iterate I use i = 0, and then length - 1 so I do not overrun the array and get a gibberish result from random memory, loops are quite easy once you get use to them!
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
SpaceCowboy

SpaceCowboy

    Newbie

  • Members
  • Pip
  • 8 posts
great info thenk you very much...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users