Create a dynamic array and assign random integer values to it? - c

int* create_array(char category, int n){
int *a;
a = malloc(n* sizeof(int));
for (int i = 0; i < n; i++) {
int x;
srand( time(NULL));
x = rand();
a[i] = x;
}
When I print this code, it just prints the same random variable 'n' times.

You can use srand(getpid() or you can specify ther range of random numbers using x=rand()%11 generates from 0-10;

You can try something like this:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int *create_array(char category, int n);
int
main(int argc, char const *argv[]) {
time_t t;
int *array;
int i, n;
char category;
srand((unsigned)time(&t));
printf("Enter category: ");
if (scanf("%c", &category) != 1) {
printf("Invalid category.\n");
exit(EXIT_FAILURE);
}
printf("Enter n numbers: ");
if (scanf("%d", &n) != 1) {
printf("Invalid n value.\n");
exit(EXIT_FAILURE);
}
array = create_array(category, n);
printf("Your n random numbers between 0-10:\n");
for (i = 0; i < n; i++) {
printf("%d ", array[i]);
}
free(array);
return 0;
}
int
*create_array(char category, int n) {
int *array;
int i, candidate;
array = malloc(n * sizeof(*array));
for (i = 0; i < n; i++) {
candidate = rand() % 10;
array[i] = candidate;
}
return array;
}

Related

How to print elements from another function into main function

I can't figure out how to print array elements from my function into the main program so if some can examine this code and help me fix it I would appreciate it. The program is supposed to take the length of the array from user input and then ask for its elements and print them out afterward.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int arrayN(int N) {
printf("Input array lenght: ");
scanf("%d",&N);
if(N>2) {
return N;
} else {
return 0;
}
}
int arrayelements(int array[], int array_length) {
int loop, i, N;
array_length = arrayN(N);
printf("Enter elements of the array: \n");
for(int i = 0; i < array_length; ++i) {
scanf("%d", &array[i]);
}
for(loop = 0; loop < array_length; loop++) {
printf("%d ", array[loop]);
}
}
int main() {
int N, array[], array_length;
int b = arrayelements(array[], array_length);
int a = arrayN(N);
printf("Array length is: %d \n", a);
printf("Elements of array are: %d \n", b);
return 0;
}
I reworked your example code. Hope it is what you want.
Focus lied on fixing the array declaration issues, memory allocation and
user input.
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
int userinput_integer(const char *fmt, ...){
int N, rv = 0;
va_list va;
va_start(va, fmt);
vprintf(fmt, va);
while(1){
rv = scanf("%d", &N);
if (1 == rv) break;
printf("Input error! The input >>");
do{
rv = fgetc(stdin);
if (isprint(rv)) putchar(rv);
}while(rv != EOF && rv != '\n');
printf("<< is not a valid integer.\nPlease try again: ");
}
va_end(va);
return N;
}
int userinput_arraylength(void) {
int N;
N = userinput_integer("Input array lenght: ");
if(N>2) {
return N;
} else {
printf("Invalid length\n");
return 0;
}
}
int userinput_arrayelements(int *array, int N) {
printf("Enter elements of the array: \n");
for(int i = 0; i < N; ++i) {
array[i] = userinput_integer("%d: ", i);
}
return N;
}
void print_arrayelements(int *array, int N){
for(int i = 0; i < N; ++i) {
printf("%d ", array[i]);
}
}
int main() {
int N, *array;
N = userinput_arraylength();
array = malloc(N * sizeof(*array));
if (NULL == array){
printf("Allocation error!\n");
exit(-1);
}
N = userinput_arrayelements(array, N);
printf("Array length is: %d \n", N);
printf("Elements of array are:\n");
print_arrayelements(array, N);
free(array);
return 0;
}
Fistly, declaration of array is not correct.
It should be array[] = {0}
Secondly, you cannot call your array elements function before arrayN function, the size of array should be entered first
And in the array elements() there is no need to call the size function you can directly pass the size of array when calling the array elements ()
Here's the code:
#include <stdio.h>
#include<malloc.h>
int *getarray()
{
int size;
printf("Enter the size of the array : ");
scanf("%d",&size);
int *p= malloc(sizeof(size));
printf("\nEnter the elements in an array");
for(int i=0;i<size;i++)
{
scanf("%d",&p[i]);
}
return p;
}
int main()
{
int *ptr;
ptr=getarray();
int length=sizeof(*ptr);
printf("Elements that you have entered are : ");
for(int i=0;ptr[i]!='\0';i++)
{
printf("%d ", ptr[i]);
}
return 0;
}

Functions and arrays which allows you to print what's the most repeated number

I have a problem with my code, it doesn't print the result I expect. This code allows the user to enter as many numbers as he wishes and then print the most repeated one
Here it is:
#include <stdio.h>
void reading_numbers(int array[]){
int i = 0;
int Max = 0;
printf("How much long the array will be?\n");
scanf("%d", &Max);
while (i < Max) {
printf("Insert the numbers\n");
scanf("%d", &array[i]);
i++;
}
}
void most_present_number(int array[], int Max){
int i = 0;
reading_numbers(array);
int current_number = array[i];
int current_number_count = 0;
int most_present_one = 0;
int most_present_one_counter = 0;
while (i < Max) {
if (array[i] == current_number) {
current_number_count++;
i++;
} else {
if (current_number_count > most_present_one_counter){
most_present_one = current_number;
most_present_one_counter = current_number_count;
}
current_number_count = 1;
}
}
printf("This is the most present number %d it is repeated %d times\n", most_present_one,
most_present_one_counter);
}
int main() {
int Max = 0;
int array[Max];
most_present_number(array, Max);
return 0;
}
The problem for me is when I call the function, but I don't know how to fix it
I should have written as a premise but I'm a bit new to C so probably there are some things in this code that don't make sense
I make a procedure to find the result ,int main() must have the size of array (mistake logic),because the procedure take the parameters of the main function (int main ())
Here is my code:
#include<stdio.h>
void most_present_number(int Max,int T[Max])
{
int i = 0;
while (i < Max)
{
printf("Insert the numbers :");
scanf("%d", &T[i]);
i++;
}
int k=0,cpt1=0,cpt=0;
for(int i=0;i<Max;i++)
{
cpt=0;
for(int j=i+1;j<Max;j++)
{
if(T[i]==T[j])
{
cpt++;
}
}
if(cpt>=cpt1)
{
cpt1=cpt;
k=T[i];
}
}
printf("This is the most present number %d it is repeated %d times\n",k,cpt1+1);
}
int main()
{
int Max = 0;
do
{
printf("How much long the array will be?\n");
scanf("%d", &Max);
}while(Max<1);
int T[Max];
most_present_number(Max,T);
}
the following proposed code:
cleanly compiles
performs the desired functionality
only includes header files those contents are actually used
and now the proposed code:
#include <stdio.h>
void reading_numbers( int Max, int array[ Max ][2])
{
for( int i = 0; i < Max; i++ )
{
printf("Insert the numbers\n");
scanf("%d", &array[i][0]);
array[i][1] = 0;
}
}
void most_present_number( int Max, int array[ Max ][2] )
{
for( int i=0; i < Max; i++ )
{
for( int j=i; j<Max; j++ )
{
if ( array[i][0] == array[j][0] )
{
array[i][1]++;
}
}
}
int most_present_one = array[0][0];
int most_present_one_counter = array[0][1];
for( int i=1; i<Max; i++ )
{
if( most_present_one_counter < array[i][1] )
{
most_present_one = array[i][0];
most_present_one_counter = array[i][1];
}
}
printf("This is the most present number %d it is repeated %d times\n",
most_present_one,
most_present_one_counter);
}
int main( void )
{
int Max = 0;
printf("How much long the array will be?\n");
scanf("%d", &Max);
int array[Max][2]; // uses variable length array feature of C
reading_numbers( Max, array );
most_present_number( Max, array );
return 0;
}
a typical run of the code:
How much long the array will be?
4
Insert the numbers
1
Insert the numbers
2
Insert the numbers
3
Insert the numbers
2
This is the most present number 2 it is repeated 2 times

How can I pass values from one function to another?

I have created a program that takes in input "n" numbers that the user chooses and then prints the most repeated one, but I have a problem with passing the values between the functions so it gives me 0 as a result. How can I solve it?
void most_present_number(int array[]);
int read_numbers(int array[]);
int main() {
int array[400];
most_present_number(array);
return 0;
}
void most_present_number(int array[]){
read_numbers(array);
int i = 0;
int Max = 0;
int Current_number = vettore[0];
int Current_number_counter = 0;
int most_present_number = 0;
int most_present_number_counter = 0;
while (i < Max) {
if (array[i] == Current_number) {
Current_number_counter++;
i++;
} else {
if (Current_number_counter > most_present_number_counter){
most_present_number = Current_number;
most_present_number_counter = Current_number_counter;
}
Current_number = array[i];
Current_number_counter = 1;
i++;
}
}
printf("The most present number is %d which is repeated %d times\n", most_present_number,
most_present_number_counter);
}
int read_numbers(int array[]){
int Max = 0;
int i = 0;
printf("Insert the array lenght\n");
scanf("%d", &Max);
while (i < Max) {
printf("Insert the numbers\n");
scanf("%d", &array[i]);
i++;
}
return Max;
}
You have Max = 0 in most_present_number(), so the while loop stops immediately.
read_numbers() returns Max, so you can use this to initialize Max in most_present_number().
void most_present_number(int array[], int Max);
int read_numbers(int array[]);
int main() {
int array[400];
int size;
most_present_number(array);
return 0;
}
void most_present_number(int array[]){
int Max = read_numbers(array);
int i;
int Current_number = array[0];
int Current_number_counter = 0;
int most_present_number = 0;
int most_present_number_counter = 0;
for (i = 0; i < Max; i++) {
if (array[i] == Current_number) {
Current_number_counter++;
} else {
if (Current_number_counter > most_present_number_counter){
most_present_number = Current_number;
most_present_number_counter = Current_number_counter;
}
Current_number = array[i];
Current_number_counter = 1;
}
}
printf("The most present number is %d which is repeated %d times\n", most_present_number,
most_present_number_counter);
}
int read_numbers(int array[]){
int Max = 0;
int i = 0;
printf("Insert the array lenght\n");
scanf("%d", &Max);
while (i < Max) {
printf("Insert the numbers\n");
scanf("%d", &array[i]);
i++;
}
return Max;
}
Note also that your algorithm assumes that all the equal numbers will be together in the array. If they can be mixed up, you need a very different design. You need another array where you keep the counts of each number. Then at the end you find the entry in this array with the highest count.

Unable to get result for dot product C programing

If I print result inside my function, I get the correct answer but if I print the result in main, I get 1. What am I doing wrong?
#include <stdio.h>
int dotpro(int v1[], int v2[], int result, int n);
int main(void) {
int i, n;
printf("Enter n: ");
scanf("%d", &n);
int v1[n], v2[n], result;
printf("Enter arr1: ");
for(i= 0; i < n; i++)
scanf("%d", &v1[i]);
printf("Enter arr2: ");
for(i= 0; i < n; i++)
scanf("%d", &v2[i]);
dotpro(v1, v2, result, n);
// enter code here
}
int dotpro(int v1[], int v2[], int result, int n) {
int i;
for (i = 0; i < n; i++) {
result += (v1[i] * v2[i]);
}
printf("%d", result);
}
You are forgetting to return the result and are attempting to pass the result parameter by value instead of by pointer reference.
Instead of this:
int dotpro(int v1[], int v2[], int result, int n) {
int i;
for (i = 0; i < n; i++) {
result += (v1[i] * v2[i]);
}
printf("%d", result);
}
Do this:
int dotpro(int v1[], int v2[], int n) {
int i;
int result = 0;
for (i = 0; i < n; i++) {
result += (v1[i] * v2[i]);
}
return result;
}
And adjust the declaration at the top of the file to match:
int dotpro(int v1[], int v2[], int n);
Then in main, invoke as follows:
result = dotpro(v1, v2, n);
printf("result = %d\n", result);

Using array of struct in a function?

I'm trying to write a function that changes one value of the elements in an array of struct, but it isn't working, the function does nothing. What am I doing wrong?
Input:
300
9
1999
1050
301
5
2000
1200
20
Expected output:
300 1260
Actual output: nothing
#include <stdio.h>
typedef struct
{int codice;
int mese;
int anno;
int stipendio;}
dipendente;
void aumento (dipendente a[], int dim, int n){
int i;
for (i=0; i<dim; i++)
{if (a[i].anno<2000) a[i].stipendio=a[i].stipendio+(a[i].stipendio*n)/100;;
if (a[i].anno==2000)
{if (a[i].mese<5)
a[i].stipendio=a[i].stipendio+(a[i].stipendio*n)/100;}}
}
int main () {
int i;
int p;
dipendente a[2];
for (i=0; i<2; i++){
scanf("%d",&a[i].codice);
scanf("%d",&a[i].mese);
scanf("%d",&a[i].anno);
scanf("%d",&a[i].stipendio);
}
scanf("%d", &p);
aumento (a, 2, p);
for (i=0; i<2; i++)
{if(a[i].stipendio>1200)
printf("%d %d", a[i].codice, a[i].stipendio);}
return 0; }
There two problems.
As #n.m. pointed out in comments: if (a[i].anno=2000) is doing an assignment and is always true (because 2000 is true). You want to compare. Use double == for it if (a[i].anno == 2000)
As #SamiHult pointed out in comments: n/100 will always be 0 for any 0 <= n && n < 100, because n is an int. Use double or float to have floating point math. Or as #alk pointed out, you can first multiply then divide, so that you can stay in integer math (a[i].stipendio * n) / 100
This is good code, but indentation just hurts.
After fixing those errors:
#include <stdio.h>
typedef struct {
int codice;
int mese;
int anno;
int stipendio;
} dipendente;
void aumento(dipendente a[], int dim, int n) {
int i;
for (i = 0; i < dim; i++) {
if (a[i].anno < 2000) {
a[i].stipendio = a[i].stipendio + a[i].stipendio * ((double)n / 100);
}
if (a[i].anno == 2000) {
if (a[i].mese < 5) {
a[i].stipendio = a[i].stipendio + a[i].stipendio * ((double)n / 100);
}
}
}
}
int main() {
int i;
int p;
dipendente a[2];
for (i = 0; i < 2; i++){
scanf("%d", &a[i].codice);
scanf("%d", &a[i].mese);
scanf("%d", &a[i].anno);
scanf("%d", &a[i].stipendio);
}
scanf("%d", &p);
aumento(a, 2, p);
for (i = 0; i < 2; i++) {
if (a[i].stipendio > 1200) {
printf("%d %d", a[i].codice, a[i].stipendio);
}
}
return 0;
}
your code prints the expected output.

Resources