Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
so i want to know if i can make an array that i can enter to it how many numbers i want, then i want to print the numbers,so i can use them later to find how many times each number was reapeted ...(sorry for my english :/ )

What I have tried:

#include<stdio.h>
#include< >

(i still don't know if it is possible to do this with array :| )
Posted
Updated 1-May-16 13:12pm

1 solution

If you want an array of dynamic size you should look into the methods malloc() and calloc() defined in
C++
<stdlib.h> and <malloc.h>


However, both these methods require a known size at initialization, so if you need to expand your array, realloc() would be useful.

You could start with a fixed length array and write the rest of your code first, like the duplicate finder and print function, and then switch to a dynamic array when you have every thing working.

C++
int fixed_length[10];

// Allocates memory for 10 integers
int* dynamic_length = (int*)calloc(10, sizeof(int));

// Allocate space for 10 mor integers
dynamic_length = (int*)realloc(dynamic_length, 10 * sizeof(int));

// Don't forget to free the memory after usage
free(dynamic_length);  
 
Share this answer
 
Comments
[no name] 1-May-16 19:30pm    
thank you! unfortunatly i can use only , stdbool.h ,stdio.h,stdlib.h :( , maybe i am suposed to unswer the question without using array :\ ,
can i tell what my question is and you tell me if i have to use arrays or not ?
[no name] 1-May-16 20:00pm    
i guess no lol
Sergey Alexandrovich Kryukov 1-May-16 20:10pm    
It's not "no" and not "yes". The whole question makes no sense. To use what for? There is no just "have to use" or not...
—SA
Sergey Alexandrovich Kryukov 1-May-16 20:10pm    
...
George Jonsson 1-May-16 20:05pm    
Read my solution once more. alloc and calloc are located in stdlib.h, so you are in luck.
Check out C Library stdlib.h[^]

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900