Printing random array element in C89 [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have the following code that works fine:
#include <stdio.h>
#include <stdlib.h>/*need this for rand()*/
#include "random.h"
#include <time.h>/*for time() function*/
int main()
{
int arr[5], a = 0;
printf("enter 5 array elements\n");
scanf("%d", &arr[0]);
scanf("%d", &arr[1]);
scanf("%d", &arr[2]);
scanf("%d", &arr[3]);
scanf("%d", &arr[4]); /*scan all elements*/
srand(time(NULL)); /*set the seed*/
a = arr[rand() % ARR_SIZE(arr)];/*from .h file*/
printf("%d\n",a);/*print the random element generated above*/
return 0;
}
It picks a random integer for an array of 5 integers.
I need the following modifications to it:
A function should accept two parameters — an array of void pointers and the array length. It should return a void pointer.
The function should pick an element from the array at random and return it.
int main() must seed the random number generator and then call the function. Then finally it should print the random element that was generated.
I don't know how to modify it to meet above requirements.
Here are the random.h file contents:
#define ARR_SIZE(arr) ( sizeof((arr)) / sizeof((arr[0])) )

This should do the trick.
void* getRandomElement(void** array, int size)
{
int* arr = (int*)*array;
return (void*)&(arr[rand() % size]);
}
int main(void)
{
int arr[5];
void* p = (void*)arr;
printf("enter 5 array elements\n");
scanf("%d", &arr[0]);
scanf("%d", &arr[1]);
scanf("%d", &arr[2]);
scanf("%d", &arr[3]);
scanf("%d", &arr[4]); /*scan all elements*/
srand(time(NULL)); /*set the seed*/
printf("random element %d\n", *(int*)getRandomElement(&p, ARR_SIZE(arr)));
return 0;
}

Related

Almost complete beginner to C. Please explain how can I solve this problem [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So the problem is:" Create a program, that inputs the elements(integers) of a matrix a[n][m] and finds the element with the smallest value amongst the the elements with the biggest value of every column."
So the way I understand it, I have to go through every column and find the biggest number in each, so I will end up with 4 numbers. From these 4 numbers I have to print out the smallest.
int main()
{
int n,m;
int A[n][m];
int B[m];
int max;
int min;
int i,j;
printf("Enter a number for n: ");
scanf("%d",&n);
printf("Enter a number for m: ");
scanf("%d", &m);
for(i=1; i<=n;i++)
{
for(j=1; j<=m;j++)
{
printf("Enter a number [%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
}
}
for(j=1; j<=m; j++)
{
max=A[j][1];
for( i=1; i<=n; i++)
{
if(max<A[j][i])
{
max=A[j][i];
B[j]=max;
}
}
}
min=B[1];
for(i=1; i<=m; i++)
{
if(min>B[i])
{
min=B[i];
printf("%d ", B[i]);
}
}
printf("Min is: %d\n", min);
return 0;
}
My question is where would the error be? It must be a logical one.
I apologize for not asking my question as clearly as most of you in this forum, but it's my first time asking here. Please if you answer, explain it like you would to a beginner.
Thanks!
C programs are generally executed from top to bottom. Therefore you cannot do this:
int n,m;
int A[n][m];
Because n and m has not been given any values. To give them values further down doesn't mean that the line int A[n][m]; will somehow get re-executed.

with in C, access variable in structure [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have the following code, which is not how to implement in C, (if it has one, the keyword with.On the other hand because the program does not access the data type structure, it should not give an error because the variables partial_n2, final_n2, name2 are defined in the struct.
The program has to store in an array of records the names of the students, their partial and final grades. Find the average grade and show a message of SUIT if the student exceeds or equals the grade of 5 or NOT SUIT if it is not enough. Do it for a number of 5 students.
#include <stdio.h>
#include <windows.h>
#include <conio.h>
//PROGRAM EJER009
#define numstudents 5
typedef struct notas{
char name2[20];
float partial_n2, final_n2;
}tnotas;
tnotas notas[numstudents];
tnotas clase;
char name[20];
float partial_n, final_n, n_media;
int i;
int main(){
for (i = 0; i <= numstudents;i++)
{
printf("Enter the student's name% d: ",i);
scanf("%s",name);
printf("Enter your partial note: ");
scanf("%f",&partial_n);
printf("Enter your final note: ");
scanf("%f",&final_n);
printf("\n");
with (clase[i])
{
partial_n2 = partial_n;
final_n2 = final_n;
name2 = name;
}
}
printf("cls");
printf("NAME\tPartial\tFinal\tMedia\tQUALIFICATION\n");
for (i = 1; i<=numstudents;i++){
with clase[i]
{
n_media = (partial_n2 + final_n2) / 2;
printf("%d %d %d",name2,partial_n2,final_n2);
system("color 14"); printf("%lf",n_media);
if (n_media >= 5)
{
system("color 11");
printf("SUITABLE :-)");
}
else
{
system("color 1");
printf("NOT SUITABLE :-(");
}
system("color 7");
}
}
getch();
return 0;
}
You can read a value of a member by:
float f;
f = notas[0].partial_n2;
You can write a value of a member by:
notas[0].partial_n2 = 10.3;

Find the largest number in array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I try to find largest number using array and pointer, but program gives errors on "finding step". Can you tell me where is my mistake?
void find_two_largest(int a[], int n, int *largest)
{
int i;
for(i=0;i<n;i++)
{
printf("enter %d. value: ",i+1);
scanf("%d",&a[i]);
}
int max=a[0];
for(i=0;i<n;i++)
if(a[i]>max) max=a[i];
largest=&max;
printf("%d",*largest);
}
int main()
{
int n,i,a[100],*lar=NULL;
printf("how many elements you want to store?\t");
scanf("%d",&n);
find_two_largest(a, n, lar);
return 0;
}
The main program is not initializing *lar, the find_two_largest function is initializing max before set user values, so max could be any value in memory.
void find_two_largest(int a[], int n, int *largest)
{
int i,max;
for(i=0;i<n;i++)
{
printf("enter %d. value: ",i+1);
scanf("%d",&a[i]);
}
//initialize max after entering values
max=a[0];
for(i=0;i<n;i++)
if(a[i]>max) max=a[i];
*largest=max;
printf("%d",*largest);
}
int main()
{
int n,i,a[100],*lar,*slar;
//initialize;
*lar=INT_MIN;
printf("how many elements you want to store?\t");
scanf("%d",&n);
find_two_largest(a, n, lar);
return 0;
}
Remove * from lar definition.
like this it should be :
int n,i,a[100], lar,*slar;
and then add an & to function call :
find_two_largest(a, n, &lar);
and after that put max = a[0] after a[0] value set.

Largest value in array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've been asked to write a program that accepts a list of numbers until a non-numeric is encountered (up to 30 numbers), putting the numbers into an array, and keeping track of how many numbers were inserted. Then it should scan through the array to find the largest number, and print the largest.
This is what I've come up with:
#include<stdio.h>
int main()
{
const int INPUT = 30 ;
int size [INPUT];
int i, big;
printf("Type integer numbers, followed by q to quit: ");
while (scanf("%d", &size[INPUT]) != 'q')
{
for(i=0;i<size;i++)
scanf("%d",&INPUT[i]);
big = INPUT[0];
for(i=1;i<size;i++)
{
if(big<INPUT[i])
big=INPUT[i];
}
printf("The largest number is %d",big);
return 0;
}
Besides the problems, I listed in the comments. You seems to be comfused by the varaible names~ Anyway, I made some code for you.
#include<stdio.h>
int main()
{
const int MAX_INPUT = 30 ;
int input[MAX_INPUT];
int size=0, big;
printf("Type integer numbers, followed by q to quit: ");
while(size < MAX_INPUT){
if(scanf("%d", &input[size]) != 1){
break;
}
++size;
}
if(size ==0){
return 0;
}
big = input[size-1];
while( size-- > 0)
{
if(big<input[size]){
big=input[size];
}
}
printf("The largest number is %d\n",big);
return 0;
}
Tested with GCC 4.1.2 and Linux.
Return value of scanf:
Upon successful completion, these functions return the
number of successfully matched and assigned input items
further, you are mixing the size and input, you actually want the size to be a constant and input to be an array:
const int SIZE = 30 ;
int input[SIZE];
So the while loop should look like:
while (scanf("%d", &input[some_index]) == 1)
and of course this is wrong:
scanf("%d",&INPUT[i]); // should be ==> &input[i]

Define a function to get the elements of 2D matrix from user in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I can't finish this code please help me! I have to matrix, and the program reads items of matrix in function
int main(int argc, char *argv[])
{
int r1,c1;
printf("Enter dimension of first matrix :");
scanf("%d %d",&r1,&c1);
int m1[r1][c1];
GetArray(m1,r1,c1);
system("PAUSE");
return 0;
}
void GetArray(int arr[][],int _row,int _column)
{
int i,j,num;
printf("Enter number: ");
for(i==0;i<_row;i++){
for(j==0;j< _column;j++){
scanf("%d",&num);
arr[i][j]=num;}} //give error in this line
}
In C programming, == is used for comparison and = is used for assignment operations. You would definitely want to assign values to j and i in your for loops. In your case, you are not initializing the loop variables (when you declare them in the beginning of the function) and since they get garbage values when they are not initialized, you try to reach beyond the bounds of the array you are using in the for loops, thus getting a segmentation fault.
int main(int argc, char *argv[])
{
int r1,c1;
printf("Enter dimension of first matrix :");
scanf("%d %d",&r1,&c1);
int **m1;
for(int i = 0; i<r1 ;++i) // use c99 !
m1[i] = malloc(c1* sizeof(int));
GetArray(m1,r1,c1);
system("PAUSE");
for(int i = 0; i<r1 ;++i)
free(m1[i]);
return 0;
}
void GetArray(int ** m1,int _row,int _column)
{
int i,j,num;
printf("Enter number: ");
for(i=0;i<_row;i++){
for(j=0;j< _column;j++){
scanf("%d",&num);
m1[i][j]=num;}} //give error in this line
}
Untested

Resources