C Program bigneer - c

I am working on generate 100 random numbers and place them in a , numbers should be 0-999. I wrote my program and it didn't print the random numbers.
I appreciate any help.
this is my code
#include <stdio.h>
#defin S 100
int main()
{
int x;
int a [S];
a[S]=100;
for(x=0;x<s;x++){
printf(a[x]);
}
return 0;
}

Two things: first, int a [S]; a[S]=100 exceeds the array bounds (max is S-1).
Second, printf(const char* format, ...) expects a format string, but you pass an integer value at the place of the format string (turn on compiler warnings!). So write printf("%d ", a[x]), and the program should at least print out some numbers (once you actually assign any numbers to a).

like this:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define SIZE 100
#define RANGE 1000
int main(void){
srand(time(NULL));
int a[SIZE];
bool chosen[RANGE] = {0};
for(int i = 0; i < SIZE; ++i){
int select = rand() % RANGE;//select 0..RANGE-1
while(chosen[select]){//check duplicate
if(++select == RANGE)
select = 0;//reset
}
chosen[select] = true;//selected
a[i] = select;
}
//result
for(int i = 0; i < SIZE; ++i)
printf("%d ", a[i]);
puts("");
return 0;
}

Related

Int variable subtraction affecting for loop index

I am using a for loop to walk through the elements of char array teste. Within this loop, I am using an int variable called stack_index, for some controls. The program below runs fine, see that, its result is the value of stack_index repeated 18 times (size of char array teste).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
size_t char_size (char* c) {
return strlen(c);
}
int main() {
char teste[] = "Este *teste* string";
size_t n = char_size(teste);
char stack[20];
int stack_index = 0;
for(int i; i < n; i++){
printf("%d", stack_index);
}
}
Result of program above:
0000000000000000000
But, when I add just the line stack_index-- inside the loop, the loop index breaks in some crazy way. See that the result of this program is the values -1, -2 and -3. This result is indicating that the for loop is running only 3 times (instead of 18). But why this is happening? Why subtracting (or adding) 1 from the stack_index variable at each iteration, is affecting the loop index variable i? This makes no sense to me, since the i variable is the index variable of the loop, not stack_index.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
size_t char_size (char* c) {
return strlen(c);
}
int main() {
char teste[] = "Este *teste* string";
size_t n = char_size(teste);
char stack[20];
int stack_index = 0;
for(int i; i < n; i++){
stack_index--;
printf("%d", stack_index);
}
}
Result of program above:
-1-2-3
#Some programmer dude comment answer the problem. I did not initialize the i variable with some initial value (like 0). This mistake was breaking the loop, not the stack_index-- line.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
size_t char_size (char* c) {
return strlen(c);
}
int main() {
char teste[] = "Este *teste* string";
size_t n = char_size(teste);
char stack[20];
int stack_index = 0;
for(int i = 0; i < n; i++){
stack_index--;
printf("%d", stack_index);
}
}
Result of program above:
-1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19

Is this C code acceptable for finding the no.of occurences for each integer in an array? Which I tried for short code aim

If this has any error, kindly mention it. Because I may figure out some future consequences
#include<stdio.h>
int main()
{
int a[]={1,2,3,4,5,5,4,3,4,5},count[10]={0},i;
for(i=0;i<sizeof(a)/sizeof(a[0]);i++)
{
int x;
x=a[i];
count[x]=count[x]+1;
}
for(i=0;i<sizeof(count)/sizeof(count[0]);i++)
{
if(count[i]!=0)
{
printf("\n %d:%d",i,count[i]);
}
}
}
Generally? No. In this specific case? Maybe.
Instead of sizeof(a)/sizeof(a[0]), use a macro to get the array size.
Dont declare/initialize a loop variable and two arrays in one line.
You will get out of bounds issues as soon as a contains a number that is bigger than the length of count - 1 or smaller than 0.
I would do something like the following:
#include <stdio.h>
#include <string.h>
#define ARRAY_COUNT(array) (sizeof(array)/sizeof(array[0]))
void GetCountOfNumberInArray(int intArray[],
unsigned int intArraySize,
int numbersToCount[],
unsigned int numberCount[],
unsigned int numbersToCountSize){
memset(numberCount, 0, numbersToCountSize * sizeof(numbersToCountSize));
unsigned int count = 0;
for(unsigned int i = 0; i < intArraySize; i++){
for(int j = 0; j < numbersToCountSize; j++){
if(numbersToCount[j] == intArray[i]){
numberCount[j]++;
break;
}
}
}
}
int main(int argc, char *argv[]){
int intArray[] = {1,2,3,4,5,5,4,3,2,1};
int numbersToCount[] = {1,2,3,4,5,6,7,8,9,10};
unsigned int numberCount[ARRAY_COUNT(numbersToCount)];
GetCountOfNumberInArray(intArray,
ARRAY_COUNT(intArray),
numbersToCount,
numberCount,
ARRAY_COUNT(numbersToCount));
for(unsigned int i = 0; i < ARRAY_COUNT(numbersToCount); i++){
printf("number %i appears %u times in the array\n", numbersToCount[i], numberCount[i]);
}
}
That way you get a universal function to count a set of numbers in an array that still works similar to your original solution.

Why doesn't my compiler generate N number of random numbers when I use 10^6 but is alright with 10^5?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(int argc, char* argv[])
{
int r = atoi(argv[1]);
unsigned int N = atoi(argv[2]);
int n[N];
int i = 0;
int rando = 0;
int average = 0;
int stddev = 0;
for(i = 0; i < N; i++)
{
rando = rand () % r;
n[i] = rando;
average += rando;
stddev += pow(rando, 2);
}
for(i = 0; i < N; i++)
printf("Array Element %d:[%d] \n", i, n[i]);
printf("The average of all numbers is %d\n", average / N);
printf("The standard deviation of all numbers is %lf\n", sqrt(stddev/N));
}
I'm generating random integers such that I enter random numbers between 1 and r-1 N times.
So, on my command line I type ./myprogram 10 1000000 and the compiler just sort of spits out nothing.
If I were to say, type in ./myprogram 10 100000, it would work just fine, albeit a little slow.
However, with another 0 it doesn't want to cooperate, why is this?

Pointing to arrays using void function

Sorry for that title. I really didn't know how to define this problem.
I was needed to declare integer array of N numbers and to fill it with random nums in void function. Then that array needs to be printed in main. The thing is that i am not allowed to use printf in void function so only way to print in main is to use pointers I guess. My knowledge is limited as I am beginner at pointers. Thx in advance and sorry for bad english.
Here is my code so far. When I compile it marks segmentation error.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void form();
int main()
{
int N, a[100];
printf("Input index: \n");
scanf("%d", &N);
form(N, &a);
printf("Array: \n");
for (int i = 0; i < N; i++) {
printf("a[%d] = %d", i, a[i]);
}
}
void form(int N, int *ptr[100])
{
srand(time(NULL));
for (int i = 0; i < N; i++) {
*ptr[i] = rand() % 46;
}
There are several issues in your code.
1) Your array decalaration form() is obsolete. Use proper prototype.
2) For declaring a VLA, declare it after reading N instead of using a fixed size array.
3) An array gets converted into a pointer to its first element when passed to a function. See: What is array decaying?
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void form(int, int*); /* see (1) */
int main(void) /* Standard complaint prototype for main.
If you need to pass arguments you can use argc, and argv */
{
int N;
printf("Input size: \n");
scanf("%d", &N);
int a[N]; /* see (2) */
form(N, a); /* see (3) */
printf("Array: \n");
for (int i = 0; i < N; i++) {
printf("a[%d] = %d", i, a[i]);
}
}
void form(int N, int *ptr) { /* Modified to match the prototype
srand(time(NULL));
for (int i = 0; i < N; i++) {
ptr[i] = rand() % 46;
}
}
So a couple things:
void form();
As Olaf was alluding to, this declaration is incorrect - you are missing the applicable parameters. Instead, it should be
void form(int N, int ptr[100]);
The main reason your program is crashing is because of the following line:
*ptr[i] = rand() % 46;
You are dereferencing the pointer at i, which is actaully giving you a number - what you want is to assign the value of the pointer at i the new random value:
ptr[i] = rand() % 46;
As related reading, see this question about passing an array in as a function parameter (basically, int ptr[] is the same thing as int * ptr)
Small modifications on your code:
1) Correction and simplification of parameter handling at function call. Just hand over "a", it's an array, so it is an address, you can use int *ptr, or int ptr[], or int ptr[100] in the formal parameter list for it. So you can use simply ptr[i] in your function.
2) Make a prototype for function from old-style declaration providing parameter list.
3) int i; declaration before the for loop - not mandatory, depends on your compiler standard
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void form(int N, int *ptr);
int main()
{
int N, a[100];
printf("Input index: \n");
scanf("%d", &N);
form(N, a);
printf("Array: \n");
int i;
for (i = 0; i < N; i++) {
printf("a[%d] = %d", i, a[i]);
}
}
void form(int N, int *ptr)
{
srand(time(NULL));
int i;
for (i = 0; i < N; i++) {
ptr[i] = rand() % 46;
}
}

How to generate random numbers in C two colons?

I need in c code that generates two numbers in horizontally...so that i can get token numbers for my login system.
I need that i get this:
token=0.152644,0.429187
so in example i have token= and random generated numbers that have at beginning 0. and then 6 random generated numbers separated with , sign.
How to get get this in C?
I have try this code but it does not give me what i want_
#include <stdio.h>
#include <string.h>
typedef
union
{
char tmp[sizeof(unsigned long long)];
unsigned long long myll;
} ll_t;
unsigned long long llrand(void)
{
FILE *in=fopen("/dev/urandom", "r");
ll_t ll_u;
fread(ll_u.tmp, sizeof(ll_u.tmp), 1, in);
fclose(in);
return ll_u.myll;
}
int main()
{
char tmp1[64]={0x0};
char working[64]={0x0};
int i=0;
for(i=0; i< 1; i++)
{
while(strlen(tmp1) < 6)
{
sprintf(working, "%lu", llrand() );
strcat(tmp1, working);
}
tmp1[6]=0x0;
printf("%s\n", tmp1);
*tmp1=0x0;
}
return 0;
}
From output i get this:
747563
102595
Can code be simple and short?
You can use rand() function:
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
int randomNumber(int min, int max)
{
/* generate secret number between min and max: */
int res = rand() % (max-min+1) + min;
return res;
}
int main()
{
int i = 0;
srand (time(NULL));
for (i=0; i<100; i++)
printf("%d ", randomNumber(10, 1000000));
return 0;
}
That is full detail for rand():
http://www.cplusplus.com/reference/cstdlib/rand/
Here is the code that is working perfect:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n1, n2;
time_t t;
srand((unsigned) time(&t));
n1 = rand() % 1000000 + 1;
n2 = rand() % 1000000 + 1;
printf("token=0.%d,0.%d\n", n1, n2);
return 0;
}
And output is:
token=0.289384,0.930887
A propose a different approach. Instead of generating 2 numbers and format them into the output string, generate 12 different digits and put them directly in place.
srand(time(0));
char output[] = "taken=0.XXXXXX,0.YYYYYY";
for (int n = 0; n < 2; n++) {
for (int k = 0; k < 6; k++) {
output[9 * n + 8 + k] = rand() % 10 + '0';
// you might want to write a function that deals with rand() bias
}
}
puts(output);

Resources