#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.
Related
So I want to split each digit of a decimal number into an array. I have the following code:
#include <stdio.h>
int * splitNumberIntoArr(int num) {
int i = num;
int modulus,newNum;
static int arr[5];
int j = 0;
while (i > 0) {
modulus = i % 10;
newNum = i / 10;
arr[j] = modulus;
j++;
i = newNum;
};
return arr;
};
int main() {
int num;
printf("Provide a number:\t");
scanf("%d", &num);
int *arr;
arr = splitNumberIntoArr(num);
int k;
for(k = 0; k <= sizeof(arr) / sizeof(arr[0]); k++) {
printf("%d\n",arr[k]);
return 0;
};
When num is an integer consising of 3 digits, the code works how it is supposed to.
However, when the input consists of more than 3 digits, the array that is returned by the splitNumberIntoArr()
function only returns an array of length 2.
for example,
Since I am new to C, I struggle to understand why this problem even exists, taking into consideration the fact that the declared array is of length 5: static int arr[5];
Your help would be greatly appreciated.
Try something like this:
#include <stdio.h>
#include <string.h> // for memset
void splitNumberIntoArr(int num, int *arr) {
int i = num;
int modulus, newNum;
int j = 0;
while (i > 0) {
modulus = i % 10;
newNum = i / 10;
arr[j] = modulus;
j++;
i = newNum;
};
}
int main() {
int num;
scanf("%d", &num);
int arr[32];
memset(arr, -1, sizeof(arr));
splitNumberIntoArr(num, arr);
for (int k = 0; k < sizeof(arr) / sizeof(arr[0]) && arr[k] != -1; k++) {
printf("%d\n",arr[k]);
}
}
In main(), the sizeof(arr) is known, because it lies on the stack.
The program should just print out the elements of the array, which stores random integers between 10 and 30. I wanted the numbers to be different from each other, but my program isn't working, what is wrong with it? thanks
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
const int N=12;
int arr[N],i,j;
srand(time(0));
for(i=0; i<N; i++)
arr[i]=10+rand()%20;
for(i=0; i<N; i++)
{
for(j=N-1; j == 0; j--)
{
do
{
arr[i]=10+rand()%20;
if(arr[i]!=arr[j])
break;
}
while(arr[i]==arr[j]);
}
printf(">>%d\n",arr[i]);
}
return 0;
}
The fact that the numbers need to be different from one another means that they are not truly random. You can create another set of numbers with elements 10 through 30 in them. Randomize that list and pull them into your array.
C++ version:
const int begin = 10;
const int end = 30;
// creates a vector of 30-10 zeroes
std::vector<int> v(begin-end);
// fill vector with 10, 11, ..., 30.
std::iota (std::begin(v), std::end(v), begin);
// a source for random seed
std::random_device rd;
// seed this generator with 32-bit number
std::mt19937 g(rd());
// randomly shuffle a vector
std::shuffle(std::begin(v), std::end(v), g);
const int N = 12;
std::vector<int> result(v.begin(), v.begin() + N);
C version:
#include <stdio.h>
#include <stdlib.h>
// https://stackoverflow.com/a/6127606/1953079
void shuffle(int *array, size_t n)
{
if (n <= 1) { return; }
size_t i;
for (i = 0; i < n - 1; i++)
{
size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
int t = array[j];
array[j] = array[i];
array[i] = t;
}
}
int main(){
const int begin = 10;
const int end = 30;
const int N = 12;
srand(time(0));
// array that contains elements 10, 11...30
int nums[end-begin];
for(int i=0;i<end-begin; i++){
nums[i] = begin+i;
}
// randomly shuffle array
shuffle(nums, end-begin);
// take first N elements
int result[N];
for(int i=0;i<N;i++){
result[i] = nums[i];
printf("%d ", result[i]);
}
}
Thanks for the help but after some more looking I found what I was doing wrong and now works.
code :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
const int N=12;
int arr[N],i,j;
srand(time(0));
for(i=0;i<N;i++)
{
arr[i]=10+rand()%30;
}
for(i=0;i<N;i++)
{
for(j=i+1;j<N;j++)
{
if(arr[i]==arr[j])
{
do
{
arr[i]=10+rand()%30;
}
while(arr[i]==arr[j]);
}
}
printf(">>%d\t",arr[i]);
}
return 0;
}
Here is the code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int IndexOfMaxInRange(int ra[], int first, int last)
{
int index = first;
int max = ra[first];
for(int i = first+1; i < last; i++)
{
if(ra[i] > max)
{
index = i;
}
}
return index;
}
void SwapElement(int ra[], int iOne, int iTwo)
{
int temp = ra[iOne];
ra[iTwo] = ra[iOne];
ra[iOne] = temp;
}
void SortArray(int ra[],int length)
{
for(int i = 0; i < length; i++)
{
SwapElement(ra, i, IndexOfMaxInRange(ra, i, (length-1)));
}
}
int main(void)
{
int ra[5] = {2,5,8,3,4};
int length = sizeof (ra) / sizeof (ra[0]);
SortArray(ra, length);
for(int i = 0; i < length; i++)
{
printf("%d ", ra[i]);
}
return(EXIT_SUCCESS);
}
I am supposed to arrange the elements from greatest to smallest, but my output is: "2 5 5 2 4"
I know I am doing something wrong, but I can't put my eye on it, thanks in advance for all the feedback.
First of all, the swap was incorrect, you lost ra[iTwo] in the process. Change to
void SwapElement(int ra[], int iOne, int iTwo)
{
int temp = ra[iOne];
ra[iOne] = ra[iTwo];
ra[iTwo] = temp;
}
Second error is that you are not updating the current max in IndexOfMaxInRange
if(ra[i] > max)
{
max = ra[i];
index = i;
}
Now it should work.
I'm learning C, and have made the following short program. It's supposed to traverse through the array, and assign each element in the array to a random value, then print them out. The only problem is, there's a compiler error stating:
called object ‘random’ is not a function or function pointer
What is the reason for this? Below is my code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int arr[5];
int i;
int random;
for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
random = rand();
arr[i] = random();
}
for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
printf("%d", arr[i]);
}
}
You have defined a variable with the same name as a function you're trying to call. This "shadows" the function and makes it impossible to call:
int random;
Quite a common error; a related popular one is when people define a function in their program called read() or send() which overrides the popular C library functions of the same names.
Since random is normal variable having type int, you won't need (and mustn't use) () operator to use its value.
Try
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int arr[5];
int i;
int random;
for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
random = rand();
arr[i] = random;
}
for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
printf("%d", arr[i]);
}
}
or more simple
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int arr[5];
int i;
for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
arr[i] = rand();
}
for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
printf("%d", arr[i]);
}
}
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;
}