How can I use information from array that will be passed from function?
#include <stdio.h>
double get_number(double[]);
main {
double x, z[100];
char m;
do {
x = get_number[z];
printf("More numbers?");
scanf (" %c", &m);
}
while ((m == 'Y')||(m == 'y'))
}
double get_number(double arr[])
{
printf ("Please enter number : ?");
scanf("%d", &arr);
return arr;
}
For example
when user press couple times Y and filled array with two or three numbers.
How can I operate with those numbers? count them or just show them.
Use a for() loop.
for (int i = 0; i < array_size; i++) {
printf ("Number at index %d: %f", i, arr[i]);
}
Of course, you need to know the size beforehand. And don't use scanf() with an array like that, why not have:
double get_input ()
{
double number = 0;
printf ("Please enter number: ");
scanf ("%d", &number);
return number;
}
Define your array length to be something fixed:
#define ARRAY_SIZE 1024
if you need more than that, increase the value or consider using a list structure.
Hope this helps
EDIT
If you want to have a sum, for example, you need to first initialize the array:
double array[ARRAY_SIZE];
for (int i = 0; i < ARRAY_SIZE; i++) array[i] = 0;
Then you get the input:
char input;
int counter = 0;
do {
if (counter > ARRAY_SIZE) break; // dont cause a segmentation fault
scanf ("%c", &input);
double number = get_input();
array[counter] = number;
} while (input == 'Y' || input == 'y');
Then when thats done, just go through the array and sum it up:
int sum = 0;
for (int i = 0; i < ARRAY_SIZE; i++) {
sum += array[i];
}
Related
#include <stdio.h>
int main ()
{
int n, i;
int arr[n];
int max1 = 0;
int max2 = 0;
// introducing array elements
printf ("Enter n: ");
scanf ("%d", &n);
for (i = 0; i < n; i++)
{
scanf ("%d", &arr[i]);
}
// calculating max1, largest number
for (i = 0; i < n; i++)
{
if (arr[i] >= max1)
max1 = arr[i];
}
printf ("The first maximum is %d\n", max1);
// calculating max2, iterate the array again
for (i = 0; i < n; i++)
{
if ((arr[i] >= max2) && (arr[i] != max1))
max2 = arr[i];
}
printf ("The second max is %d", max2);
}
The problem asks me to read an one dimension array and find the two largest numbers in this array. I tried to solve it in a less efficient way, so I will first iterate the entire array in order to find the first maximum, then iterate it again to find the second one.
My error is segmentation fault (Core dumped) and I don't understand where I did wrong.
You're not allowed to set a stack allocated array using a variable at:
int arr[n];
You are allowed to do this:
int arr[10] //or any number
Basically the size of the array has to be known at compile time.
You've also never set actually set n to a number.
Edit: This apparently incorrect in modern c you are allowed to do this.
It should be like this
#include <stdio.h>
int main ()
{
int n, i;
int max1 = 0;
int max2 = 0;
printf ("Enter n: ");
scanf ("%d", &n);
int arr[n];
// introducing array elements
for (i = 0; i < n; i++)
{
scanf ("%d", &arr[i]);
}
// calculating max1, largest number
for (i = 0; i < n; i++)
{
if (arr[i] >= max1)
max1 = arr[i];
}
printf ("The first maximum is %d\n", max1);
// calculating max2, iterate the array again
for (i = 0; i < n; i++)
{
if ((arr[i] >= max2) && (arr[i] != max1))
max2 = arr[i];
}
printf ("The second max is %d", max2);
}
note this
printf ("Enter n: ");
scanf ("%d", &n);
int arr[n];
your compiler gives an arbitrary garbage value to n, in your code and that possibly is smaller than the number of iterations you have.
I,however, doubt the working of my code in some classic compilers as they want you to put all the declarations on the top
My background is java so I'm not used to c syntax yet.
I need to do the following: let the user to input number k (number of rows), and after to insert values into 2d array with this form:
1 2
3 4
5 6
i.e two values with space between them and then new line for the new row.
If the user entered k=1000 but entered only 4 rows so the function call would be only with the array with 4 rows and not 100. the loop that reads the values should stop if: there are k rows or reaching to EOF
My questions:
I don't know how to implement the EOF part.
I don't know how to implement that for k=1000 and there only 4 rows so call the function with the array that contains only 4 rows
Here my code:
#include <stdio.h>
#define COLS 2
void foo(int** rows, int n);
int main()
{
int k;
printf("Please enter number of rows\n");
scanf_s("%d", &k);
int** matrix = (int**)malloc(k * sizeof(int*));
for (int i = 0; i < k; i++)
matrix[i] = (int*)malloc(COLS * sizeof(int));
int num1, num2;
for (int i = 0; i < k||num1!=EOF; i++)
{
printf("Enter two numbers separated by space \n");
scanf_s("%d %d", &num1, &num2);
matrix[i][0]=num1;
matrix[i][1] = num2;
}
printf("The array:: \n");
for (int i = 0; i < k; i++)
{
for (int j = 0; j < COLS; j++)
{
printf("%d \t",matrix[i][j]);
}
printf("\n");
}
foo(matrix, k);
for (int i = 0; i < k; i++)
{
free(matrix[i]);
}
free(matrix);
return 0;
}
void foo(int** rows, int n)
{
//some stuff
}
Change the bellow portion of your code:
for (int i = 0; i < k||num1!=EOF; i++)
{
printf("Enter two numbers separated by space \n");
scanf_s("%d %d", &num1, &num2);
matrix[i][0]=num1;
matrix[i][1] = num2;
}
To:
int i;
for (i = 0; i < k; i++)
{
printf("Enter two numbers separated by space \n");
if(2 != scanf_s("%d %d", &num1, &num2)) break;
matrix[i][0]=num1;
matrix[i][1] = num2;
}
k = i;
Hope it will work as you want
check the return value of scanf
for (int i = 0; i < k; i++)
{
printf("Enter two numbers separated by space \n");
if(scanf_s("%d %d", &num1, &num2)==EOF)
break;
matrix[i][0]=num1;
matrix[i][1] = num2;
}
Hi I am trying to write a code in C to find the GCD for more than 2 numbers. However, when I compile and run the code, the GCD is always 0. Would appreciate if anyone can help. Thank you.
#include <stdio.h>
static int gcd(int x, int y)
{
int r;
if (x <= 0 || y <= 0)
return 0;
while ((r = x % y) != 0)
{
x = y;
y = r;
}
return y;
}
int main (void)
{
int A[5];
int g = A[0];
int i;
int n;
printf ("How many elements are there? \n")
scanf ("%d", &n);
printf ("Input the elements. \n");
for (i = 0; i < n; i++)
{
scanf ("%d", &A[i]);
}
for (i = 1; i < n; i++)
g = gcd(g, A[i]);
printf ("GCD is: %d \n");
return 0;
}
You set g equal to A[0] before you set A[0] to any particular value.
You need a ; after the first printf.
You need to declare A with n elements after you read the number of elements n.
You have to write the g after ", in your last printf.
This is how I think your main should look like:
int main (void)
{
int i;
int n;
printf ("How many elements are there? \n");
scanf ("%d", &n);
int A[n];
printf ("Input the elements. \n");
for (i = 0; i < n; i++)
{
scanf ("%d", &A[i]);
}
int g = A[0];
for (i = 1; i < n; i++)
g = gcd(g, A[i]);
printf ("GCD is: %d", g);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
double mean(int i, int arr[])
{
int j, sum = 0;
for (j = 0; j < i; j++)
{
sum = arr[j] + sum;
}
return (float)sum/i;
}
int main()
{
int arr[100] = { NULL };
int i, n, sum = 0;
printf("How many numbers would you like to enter?");
scanf_s("%d", &n);
while (n > 100 || n < 0)
{
printf("Amount of numbers should be less than 0 and more than 100\n");
scanf_s("%d", &n);
}
for (i = 0; i < n; i++)
{
scanf_s("%d", &arr[i + 1]);
}
printf("%f", mean(i-1, arr[i]));
system("pause");
}
When I run the code it gives me a read access error. The problem is with the mean() function I created but I don't know what's wrong. Help?
When I run the code it gives me a read access error. The problem is with the mean() function
Although the mean() function produces read access error, the actual problem is here:
printf("%f", mean(i-1, arr[i]));
You are not passing an array to your function, but its element (it is one past the end of what was written, too, so even the value that you pass is undefined).
You need to pass i for the length, because your mean() treats it as an exclusive upper limit, and you also need to pass arr for the array:
printf("%f", mean(i, arr));
Indexing problem when reading the data also needs to be fixed - you need to remove + 1:
scanf_s("%d", &arr[i]);
// ^
Try this:
#include<stdio.h>
#include<stdlib.h>
double mean(int i, int arr[])
{
int j, sum = 0;
for (j = 0; j < i; j++)
{
sum = arr[j] + sum;
}
return (float)sum/i;
}
int main()
{
int arr[100] = { 0 };
int i, n, sum = 0;
printf("How many numbers would you like to enter?");
scanf("%d", &n);
getchar();
while (n > 100 || n < 0)
{
printf("Amount of numbers should be more than 0 and less than 100\n");
scanf("%d", &n);
getchar();
}
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
getchar();
}
printf("%lf", mean(n, arr));
getchar();
}
Your call to the mean function was wrong. You have to pass the entire array, not just one element. Change arr[i] to arr.
Other minor modifications are what I did to make it run on my system. If it works for you otherwise, then great.
My function countCopies doesn't work even though it gets the right inputs. All it should do is taking an array of integers as an input and then searching through this array for duplicates of the second input x.
int main() {
char intArray[100]; //The integer array can only hold 100 integers
int i, x, j;
printf("Please enter a couple of integers and when you're done enter end. ");
i = 0;
while (scanf("%d", &intArray[i++]) == 1)
/*empty loop*/;
scanf("%*s");
printf("Enter x:");
scanf("%d", &x);
printf("Copies = %d\n", countCopies(intArray, i, x));
}
int countCopies(int a[], int n, int x) {
int count = 0;
int j = 0;
for (j = 0; j < n - 1; j++) {
if (a[j] == x) {
count++;
}
}
return count;
}
The for loop is incorrect: you should change the test to j < n.
The idiomatic for loop in C: for (j = 0; j < n; j++) ... iterates exactly n times, j taking values 0 to n-1 inclusively, which are correspond to exactly all valid positions in an array of n elements.
Note that the array has the wrong element type: it should be int, not char. You should also check for array bounds in the first loop and for conversion success of the last scanf.
Here is a corrected version:
#include <stdio.h>
int countCopies(int a[], int n, int x);
int main(void) {
int intArray[100]; //The integer array can only hold 100 integers
int i, x;
printf("Please enter a series of integers, end the list with the word end.\n");
for (i = 0; i < sizeof(intArray) / sizeof(*intArray); i++) {
if (scanf("%d", &intArray[i]) != 1)
break;
}
if (scanf("%d", &x) == 1) {
printf("too many numbers\n");
return 1;
}
scanf("%*s"); /* skip the end word. Note that any word is OK */
printf("Enter x:");
if (scanf("%d", &x) == 1) {
printf("Copies = %d\n", countCopies(intArray, i, x));
}
return 0;
}
int countCopies(int a[], int n, int x) {
int j, count = 0;
for (j = 0; j < n; j++) {
if (a[j] == x) {
count++;
}
}
return count;
}
To expand on chqrlie's answer,
The code for (i = 0; i < n; i++) {/* do stuff */} executes in the following order:
The variable i is set to zero.
The conditional i < n is tested. If true, the code in /* do stuff */ is executed, otherwise, the loop is exited.
The increment i++ is applied.
Go back to 2.
That means that on the loop iteration when i gets incremented to n, the for loop breaks before the code inside the loop gets executed, so if you're accessing an array with n elements, the last element that gets accessed has the index n - 1. Hence the C loop paradigm that chqrlie mentioned.