Aia 1,977 Nearly a Posting Maven

a_variable_float = pow(a,b);

Aia 1,977 Nearly a Posting Maven

#include <math.h>

pow(base, exponent);

Aia 1,977 Nearly a Posting Maven

You can use getch() to do the same thing but without the anoying text. ;)

The function getch is a compiler especific function, meaning that is not
standard so you can not use it with every C compiler.
Stay away from it. Getchar is the standard, use it, and build good program habits from the beginning. :confused:

Aia 1,977 Nearly a Posting Maven

Hi, this is not a HUGE problem but I was jw why at the end off all my codes I have to type, system("pause");.

When I even do the Hello World! program thing the black screen just flashes. I was jw why no one elses codes have this.
Its not a big problem but i was just wondering.

When you program for a console window, the program just do what
you ask to do, and then exit returning control to the operating system.
That's why "the black screen just flashes". In orden to see what is happening you has to make the program to pause or wait for you to enter a key. System("PAUSE") achieves this but is operating system
depended. Meaning "not portable". Another way of doing it, is using the getchar function. Or you can always run the program in a terminal or command line console.

Aia 1,977 Nearly a Posting Maven
/* myconcat.c */

#include <stdio.h>

int main(void)
{
    char first_s[] = "Hello";
    char second_s[] = "World";
    int i = 0;
    int x = 0;
    
    char long_s[sizeof(first_s) + sizeof(second_s)];

    while(first_s[i])
    {
        long_s[i] = first_s[i];
        ++i;
    }
    long_s[i] = ' ';
    ++i;
    
    while(second_s[x])
    {
        long_s[i] = second_s[x];
        ++i;
        ++x;
    }
    long_s[i] = '\0'; /* don't forget the null */
    
    printf(long_s);
    putchar('\n');
    
    getchar();
    return 0;
}

If you want to learn something more advanced, do read this little snippet by Dave Sinkula.

http://www.daniweb.com/code/snippet406.html

Aia 1,977 Nearly a Posting Maven

Thanks, I wasn't sure exactly, but even with that change the output is still "22222" ???

You are doing something different then. This code works as explained.

#include <iostream>

using namespace std;

int main() {

int a[5] = {1,2,3,4,5};
int *aptr = a;

for(int i = 0; i < 5; i++)
   cout << *(aptr + i);

system("pause");
return 0;
}

I noticed that other pleople posted the solution before I finish this post. Good that is solved

Aia 1,977 Nearly a Posting Maven

int *aptr = &a[0];

When you asign a pointer to an array you don't use the & operator.

int *aptr = a;

cout << (*aptr + 1);

}

cout << *( aptr + i ) will do it. Notice the "*" outside the parenthesis, and the i to index the pointer array.

Aia 1,977 Nearly a Posting Maven

It has to do with the order of precedence.
The && evaluates before the ||.
It is always evaluating

third != fourth && iteration < 5

before anything else.
You have to use () around the operations.

Aia 1,977 Nearly a Posting Maven

twhich bit did i screw up? should there be an &mood in the ()?
i used that tutorial before and same problem still..

if you are talking about the switch(mood) you don't need to write the
& operator there.

Did you take a look at the links that Joeprogrammer posted for you?.
They will help you to understand why is not working.

If you did. Could you post the changes you did to your code? That would help a lot.

Aia 1,977 Nearly a Posting Maven

all of yur Replies are True , Sorry To say that , But Not as much usefull as i thought them to be , beacuse i knew All of them before ,

I am very sorry that we couldn't help you. I thought that by going down with what your code would do, step by step you would see that it has some problems. My bad there.

I understand that english is not your native language, and I know how hard is to express your ideas. Could you say it onces more in other words, I not quite sure what you are asking, especially this part.

to Say that which parameters should be Shown TO show that a sort algorithm Works well

parameters are the ones that you passed inside the parenthesis of the function, and they are OK. You are passing an integer array and an integer which are what you need to do the sorting. However I got the
feeling that's not what you are asking.

Let me ask you a question. What are you trying to do with the sort?.
Are you trying to sort the elements from largest to smaller?. Are you
trying to arrange them from smaller to greater?.

I commend you for going back and tagging your code to look neater. :)

Aia 1,977 Nearly a Posting Maven
for( x = 2; x < 100; x++ )

This for loop will start with the value 2 stored in x and
check every time if is less that 100, so it will never reach 100 numbers but 99, since at the 100th time it will quit. After the block {} is executed it will increment x by one, making x = 3 and so on.

if( x % 2 != 0 || x == 2 )

will execute only if: the remainder of the division
between the value of x and 2 is not 0 (zero) OR x is iquals to 2. If one or both of
the operations are true then it will keep going down.

for( y = 2; y <= x / 2; y++ )

similar to the first loop only that is nested inside
the if statement and then subject to it in the execution. Also notice the <=, meaning
that it will check upto the value of x included, not like the first for loop.

if( x % y == 0 )

problably you could figure this one out compering it with the first if.

if( y > x / 2 )

if the value store in y is greater than the result of dividing the
value store in x and 2, then it will execute the following block, which it will make stop the second for loop.


I don't …

Aia 1,977 Nearly a Posting Maven

Let's say you pass the array int A[] = { 1, 2, 3, 4 };
and that the int n is iqual to the number of elements in the array,
in this case 3 since starts at element 0.
This sorting function is going to do the following:

the for loop will start with a 1 as an indexer.

For loop checks that 1 is less or iqual to n, which has a value of 3 (four elements in the array). Is 1 <= 3?. Yes.
The block that belongs to the for loop is executed.

The while loop checks: Is it true that what A[1] (whatever value is in A[1],second element of the array; this case a 2) is not 1?. 2 != 1?. Yes.

The block belonging to the while loop is execute.

variable t gets the value of A[1] which is 2; A[1] gets A[t] which is 3 now, since
A[t] is A[2] or third array element; A[t](or A[2]) gets whatever is in t, at this time a 2.

Then while loop is finished in this round, and for loop increments n by one, making it this time a 2.

So far then we have after this round:
A[0] = 1 (the function loops will never touch this element, as is written)
A[1] = 3
A[2] = 2
A[3] = 4

Do we want to go another round with it. …

Aia 1,977 Nearly a Posting Maven

@amthwee

Thank you for those pdf files full with information.

Aia 1,977 Nearly a Posting Maven

>does the modulous(sp?) operator only work on integers?
It works on any number type as far as I know.

The modulus operator can be use only with integer operands

Aia 1,977 Nearly a Posting Maven

I'm having trouble also coming with my own solution.
It would be great if someone would post an example of:

all permutations of 2 coins

I'll keep working on it.

Aia 1,977 Nearly a Posting Maven

This is just to follow up on my previous example

Thank you for posting this code. I will study it.

However when I run it, this is the output I get:

Original string non-copied: Reverse MeÇ|¿ "
Original string copied: " ¿|ÇeM esreveR

I'm sure it can be fix very easy to not show the extra characters.

Aia 1,977 Nearly a Posting Maven

This is another way I have been playing with.

/*
 * gnirts.c
 *
 * plays with the idea of reversing a given
 * array of characters, in memory.
 */
#include <stdio.h>

void reverse_s(char *string);

int main(void)
{
    char hi[]="Daniweb is the best";
    size_t index;

  /* show me the content and memory location */
    for(index = 0; index < sizeof(hi) / sizeof(char); index++)
    {
        printf("Hi[%d] = %c living at %d memory address\n", index, hi[index], &hi[index]);
    }

  /* reverse the array in memory */
    reverse_s(hi);
    
    putchar('\n');
    
  /* show me the content and memory location after upsetting the array :) */
    for(index = 0; index < sizeof(hi) / sizeof(char); index++)
    {
        printf("Hi[%d] = %c living at %d memory address\n", index, hi[index], &hi[index]);
    }

    getchar();
    return(0);

}

/*
 * reverse_s function
 * parameter: a pointer to a string
 *
 * reverse a given string
 */
void reverse_s(char *string)
{
   char save;
    size_t i;
    
   int len = strlen(string);
   
    for(i = 0; i < (len / 2) + 1; i++)
    {
        save = string[i];
        string[i] = string[len - i];
        string[len -i] = save;
    }
}
Aia 1,977 Nearly a Posting Maven

I used your question to see if I could do it. I know is not exactly what you are asking. If I understand correctly, you want a void function, and main is not void. However, I though to post my code as another, problably not so elegant, idea.

Some of the loops are just for testing and prove that the memory addresses are the same. The "NULL" can be avoided to swap at first address if you want.

#include <stdio.h>

int main()
{
    char hi[] ="Hello";
    char ih[sizeof(hi)];
    char *hi_ptr;
    size_t i;
    int length;

    hi_ptr = hi;

  /* to prove the memory addresses */
    for(i = 0; i < sizeof(hi) / sizeof(char); i++)
    {
        printf("Memory location of hi[%d] = %d, and in it lives %c\n", i, hi_ptr++, hi[i]);
    }
    
    strcpy(ih, hi);

    length = strlen(ih);
    
  /* set the pointer again since it was moved displaying the preview addresses */
    hi_ptr = hi;
     
    for(i = 0; i < sizeof(ih) / sizeof(char); i++)
    {
        *(hi_ptr + i) = ih[length - i]; /* swap happens here */
    }
    
    putchar('\n');
    
 /* to prove memory addresses are the same */
    for(i = 0; i < sizeof(hi) / sizeof(char); i++)
    {
        printf("Memory location of hi[%d] = %d, and in it lives %c\n", i, hi_ptr++, hi[i]);
    }

    getchar();
    return(0);
}

I'm going to try now using Narue suggestion.

Aia 1,977 Nearly a Posting Maven

Just a small minor correction.
Instead of:

for(i = 1; i < number; i++)

It should be :

for(i = 1; i <= number; i++)

Sorry about that.

Aia 1,977 Nearly a Posting Maven

Here is a little different version of your code:

/*
 * factorial number.c
 * for testing purposes only.
 */
#include <stdio.h>

int main(void)
{
    char string[20]; 
    int number;    
    int fact = 1;
    int i;

    fputs("Please enter a number: ", stdout); 
    fflush(stdout);

    /* read the input as a string */
    if(fgets(string, sizeof(string), stdin))  
    {

     /* check that there is a number in the inputed string */
        if(sscanf(string, "%d", &number) == 1)
        {

           /* the loop that will display the result */
            printf("Factorial is ");
            for(i = 1; i < number; i++)
            {
                fact = fact * i;
                printf("%d ", fact);
            }
            putchar('\n'); 
        }
    }
    getchar();
    return(0);
}

sir i am still amaeture could u please helpme in this prg.

I know that feeling, however may I suggest that you read this blog?

Also, I took the liberty of finding these other helpful links for tagging and formatting C/C++.
This one is really great is you want to learn what's wrong with using
some popular functions like scanf() and gets(). Will you read them?.

Aia 1,977 Nearly a Posting Maven

Aia, what did you just post? No explanation, no formatting, improper programming practices (void main(), getch(), clrscr())
You should model good practices and proper techniques. You seem to have been here long enough to know this... ;)

@WaltP

I just tried to expose the logic of his code using some comments. Just in case he/she needed a little bit more explanation.

Infarction already mentioned about the "void main" declaration.

By the way, main should return a type int

and concerning about the getch(), clrscr() Well, knowing that they are not portable is good, however I'm not the most knowledgable guy
to preach. I don't feel the most qualified to correct those things, even when reading many of your comments has made me not to use them.

By the way, I'm suprise you haven't say anything about

#include<conio.h>.

This is a "no, no" if you want your code to be portable. :confused:

Aia 1,977 Nearly a Posting Maven
/*factirial*/
 
#include<stdio.h>
#include<conio.h>
void main()
{
int n,s=1,i; /* n = unknown garbage, s = 1, i = unknown garbage */
clrscr();
printf("enter n ");
for(i=1;i<n;i++) /* Initially loop as long as i is less than n. what was in n?. Garbage! anything can happens */
{
scanf("%d",&n); /* user set n to some integer, let's say as an example integer 4 */ 
}
for(i=1;i<n;i++)  /* Initially i= 1, n = 4; 1 < 4? Yes, execute block */ /* Will do 3 times */
{
s=s*i; /*first time s = 1 * 1 = 1 */ /* second time s = 1 * 2 = 2 */ /* third time s = 2 * 3 = 6 */
}
printf("factorial is %d",s); /* factorial is 6 */ /* This line has to be inside the loop to display each value */
getch();
}
Aia 1,977 Nearly a Posting Maven

I'm not Dave, but I'll try to help ;)

Thank you Joeprogrammer, I think I get it now. This would never work:

*string_ptr = "This is a string";

because I will be trying to fit a whole array of chars inside just one
char which is the first address that the pointer is pointing to, correct?

Aia 1,977 Nearly a Posting Maven

@ Dave Sinkula,

string_ptr = string; /* point to the uninitialized garbage */
string_ptr = "This is a string of less than 60 chars"; /* re-point to a string literal */

This is the trouble part of my code then. I thought once I pointed the
first time to the char array I could assign the string using the pointer.

I guess what I would like to know..., is there a way I could
change the value in the string array using the pointer?. Or I will always
need to initialize the array with the string?.

Aia 1,977 Nearly a Posting Maven

@ Dave Sinkula,
That little bit of sample code is going to be very helpful to me, now that I'm using pointers. Thank you!.
However I noticed that the array is a int; how do you do when is a array of chars. I'm struggling with strings. I don't know how I can use a pointer to fill an array of char or string.

This a sample of what I don't understand.
Could you give me some Pointers?.

/*
 * string_ptr.c
 * test of pointer action
 */
#include <stdio.h>

int main(void)
{
    char  string[60];   /* array of chars */
    char *string_ptr;   /* a pointer to a string??? */

    string_ptr = string; /* initialition of string pointer??? */
    string_ptr = "This is a string of less than 60 chars"; /* assign this string to the pointer */

    printf(string);   /* display the character array */
    putchar('\n');
    printf(string_ptr); /* display the string pointer */
    putchar('\n');
    
    return(0);
}
/* string display output is garbage */
/* string_ptr pointer is correct */
/* why? */
Aia 1,977 Nearly a Posting Maven

@ Dave Sinkula

Your tutorial has been a great help for me. I was going to point to it, but
I don't know how to make it a link to a frase or word. I'm very glad that
you did

Aia 1,977 Nearly a Posting Maven

What upset me sometimes is that any book or video tutorial that
tries to teach you C programming, all of them teach you to use gets() and scanf() as the default to read input. I understand that is easy for learning but it should be shown what problems you get when using those functions. As a beginner learning, you think everything is well with them. It took me to meet this fine forum to start understanding what was wrong using those functions.

I sugest you to do some search in this forum for the gets() and scanf()
and you will learn why your code has problems not stopping to read the next input from you. ;)

Aia 1,977 Nearly a Posting Maven

@ Ravalon
Thank you!, I have been trying to do that shape for some time.
I'm going thrugh your code to understand it, and I got a question in this line:

const char *content string( size_t n, const char c)

what is size_t n? I recognized that it's some kind of variable type, but I don't see it declared anywhere else in you code. Could you give me some information about it?.