How should I implement a rollDice() function in C? - c

I try to implement a function meant to roll a dice a certain amount of time.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int * rollDice(int len) //len = times the dice is rolled.
{
int ints[len];
int i = len-1;
while(i>0)
{
ints[i--] = (rand()%6)+1;
}
return ints;
}
int main(int argc, const char * argv[])
{
int * ints = rollDice(10);
for(int i =0; i<10; i+=1)
{
printf("%d ",*(ints+i));
}
return 0;
}
Program always prints this, is my conception of pointers false ?
104 0 0 0 1919706998 2036950640 1667723631 1836545636 16 48

You cannot do this
return ints;
It's declared on the stack. You need to either pass it in with enough memory or allocated the memory in the function using malloc and pass it back.
int * rollDice(int len) //len = times the dice is rolled.
{
int *ints = malloc(sizeof(int) * len);
int i = len-1;
while(i>0)
{
ints[i--] = (rand()%6)+1;
}
return ints;
}

Harry's answer is right; you can't return the address of a local variable. That variable is destroyed as soon as the function returns.
Instead of having to allocate memory in the function, just pass the array to be filled into the function:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_DICE 10
void rollDice(int *dice, int num_dice)
{
int i;
for (i = 0; i < num_dice; i++) {
dice[i] = (rand() % 6) + 1;
}
}
int main(int argc, const char * argv[])
{
int dice[NUM_DICE];
srand(time()); /* Don't forget this! */
rollDice(&dice, NUM_DICE);
for(int i = 0; i < NUM_DICE; i++)
{
printf("%d ", dice[i]); /* Easier to use brackets than pointer arithmetic. */
}
return 0;
}

Related

How does one take the end result of a loop as an int for further use?

I have written a basic loop:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
int a = atoi(argv[1]);
for (int i = a; i > 0; i = i - 7)
{
if (i < 7)
{
printf("%i", i);
}
}
}
I know how to print the end result, but I want to extract the result as an int so I can use it for calculations later on, how would I go about that?
If you declare i in the loop initializer, it'll be scoped to the for loop. So, declare it before the for loop, then assign it in the initializer:
int i;
for (i = a; i > 0; i = i - 7) {
// ...
}
printf("%i\n", i);

Why is the code printing me only the value of the last pointer in the array of pointers to a string?

Here is my code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main(){
char* a[5] = { "tomer","tomer","tomer","tomer","tomer" };
char t[] = "ppppr";
char* n = &t;
for (int i= 0;i < 3;i++) {
scanf(" %s",n);
a[i] = n;
}
for ( int j= 0;j < 3;j++) {
printf("%s\n", a[j]);
}
system("pause");
return 0;
}
What is the reason this outputs three times the last vlaue in the array?
Because you are allocating the memory for new string only once which is
char t[] = "ppppr";
BTW, there are other problems with in your code.
you are returning 0 from main but its return type is void instead of
int
you should do char* n = t; not &t

C: Printing out the value and memory location of each element of an array using pointers?

I have generated a random array inside the main function, How can I properly print it out using a separate function and inside the function print out the value and memory location of each element of that array using pointers. Here is my code so far:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void printArray(int *pointertoArray, int *Size);
int main (void)
{
srand(time(NULL));
int array[10];
int *pointer = NULL;
for(int i = 0; i < size; i++)
{
array[i] = rand();
*pointer = array[i];
printArray(*pointer,size);
}
}
void printArray(int *pointerToArray, int *size)
{
int i = 0;
do
{
printf("\nValue %d = %p ",i,*pointerToArray);
i++;
}
while(i < size);
}
Here is what I am trying to achieve:
value 1 = 0x7fff0815c0e0
.....
value 10 = 0x7fff0815c0ec
int *size should be int size. You don't pass a pointer, and you don't need a pointer.
Actually, size_t size would be more appropriate.
The call to printArray should be located after the loop. You only want to print the array once.
printArray(*pointer, size); should be printArray(array, size);.
pointerToArray should be named array or pointerToInts.
The value of the element is pointerToArray[i], not i.
The address of the element is pointerToArray+i, not *pointerToArray.
The loop in printArray should be top-tested. (No reason for it to be bottom tested, so play it safe.)
main is declared to return an int, but doesn't.
We get,
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void printArray(int *array, size_t size);
int main() {
srand(time(NULL));
int array[10];
for (int i = 0; i < size; ++i) {
array[i] = rand() % 1000;
}
printArray(array, sizeof(array)/sizeof(array[0]));
return 0;
}
void printArray(int *array, size_t size) {
for (int i = 0; i < size; ++i) {
printf("Value # %p = %d\n", array+i, array[i]);
}
}
Alternative:
void printArray(int *pointerToInt, size_t size) {
for (; size--; ++pointerToInt) {
printf("Value # %p = %d\n", pointerToInt, *pointerToInt);
}
}

C the greatest value in random array, not working properly

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int array[];
int arraySize = 460;
int max;
int array_size(int n) {
array[n];
}
int read_from_array(int n){
for(int i = 0; i <= arraySize-1;i++ )
printf("%d|",array[i]);
}
int array_generator(int n) {
for(int i = 0; i <= n; i++)
array[i] = rand() % 1000;
}
int find_max(int n) {
for(int i = 0;i <= n-1;i++)
{
if(array[i] > max)
max = array[i];
}
printf("\n%d",max);
}
int main(int argc, char** argv) {
srand(time(NULL));
array_size(arraySize);
array_generator(arraySize);
//read_from_array(arraySize);
find_max(arraySize);
return 0;
}
im learning and have made something like this to find biggest int in random array
when this variable is set < 460 it works but when i make it bigger, it wont work.
int arraySize = 460;
i want to know why this is happening and how to make it better.
Lots of stuff.
Firstly, your array is never actually allocated. You're putting all those random integers into... who knows where. The easy way to fix this is to put a number in the [] after the array declaration. But to do this, the size has to be a constant, so you can't use a variable like arraySize to set the size. You can use a preprocessor #define, though. Like so:
#define ARRAY_SIZE 460
int array[ARRAY_SIZE];
Your array_size function doesn't do anything.
All of your functions are declared to return an int, but there's no return statement. Either make them void or return something.
Your max should be a local variable inside find_max and it should be given a value before you try to use it in the if statement.
int find_max(int n) {
int max = array[0];
for(int i = 0;i < n;i++)
{
if(array[i] > max)
max = array[i];
}
printf("\n%d",max);
return max;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void array_init(int* array, int n) {
for(int i = 0; i < n; i++) {
array[i] = rand() % 1000;
}
}
int array_max(int* array, int n) {
int max = array[0];
for(int i = 0;i < n;i++)
{
if(array[i] > max) {
max = array[i];
}
}
return max;
}
int main(int argc, char** argv) {
srand(time(NULL));
int array_size = 460;
int array[array_size];
array_init(array, array_size);
int max = array_max(array, array_size);
printf("%d\n", max);
return 0;
}
max is never initialised, and may start wth a value that is larger then your array's largest value. It should be initialised thus:
#include <limits.h>
int max = INT_MIN ; // Init. to smallest possible value.

Issues with passing pointer arrays and printing them

I'm having some issues with very simple situations of passing arrays as pointers into functions and returning them. I thought I had pointers figured but I just can't get my head around it.
Here's the code:
int* getLottoDraw();
void printArray(int * array);
int find_matches(int * array1, int * array2);
int main(int argc, char *argv[])
{
int * lotteryDraw = getLottoDraw();
printArray(lotteryDraw);
system("PAUSE");
return 0;
}
int* getLottoDraw(){
int draw[6];
int i;
srand(time(NULL));
for (i = 0; i < 6; i++) {
int r = rand() % 49;
draw[i] = r;
}
return draw;
}
void printArray(int *array){
int i;
for (i = 0; i < 6; i++){
printf("%i ", array[i]);
}
}
One example output is "3 2047 4614546 0 25 45". Not what was hoping for.
You are returning a stack address, which end up being destroyed when the function ends.
Stack variables are local variables, their scope is limited to the function they're created.
They're created on the function, and destroyed when the function ends, so if you've try to access this address later you'll get undefined behavior.
You should have a dynamic allocated pointer to be able to access it outside the function, or return by value, copying the content (which can be costly in an array case).
You could do something like:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int* getLottoDraw();
void printArray(int * array);
int find_matches(int * array1, int * array2);
int main(int argc, char *argv[])
{
int * lotteryDraw = getLottoDraw();
printArray(lotteryDraw);
free(lotteryDraw);
return 0;
}
int* getLottoDraw(){
int* draw = malloc(sizeof(int)*6);
int i;
srand(time(NULL));
for (i = 0; i < 6; i++) {
int r = rand() % 49;
draw[i] = r;
}
return draw;
}
void printArray(int *array){
int i;
for (i = 0; i < 6; i++){
printf("%i ", array[i]);
}
}

Resources