This is a program to print the smallest value and its position in an array (defined by user).
#include <stdio.h>
int position_smallest(int a[],int n)
{
int smallest = a[0];
int i,k;
for(i=0; i<=n-1; i=i+1)
{
if(a[i]<a[0])
{
smallest = a[i];
k = i;
}
}
printf("The smallest value is %d\n", smallest);
printf("It's position is %d\n", k);
return 0;
}
int main()
{
int n,j;
int a[n];
printf("Enter the size of the array: ");
scanf("%d", &n);
for(j=0; j<=n; j=j+1)
{
printf("a[%d] = ", j);
scanf("%d", &a[j]);
}
position_smallest(a,n);
}
But upon running it, it shows following error:
Segmentation fault (core dumped)
What can be the possible reason(s) for it?
First error, as stated in one of the comments, is declaring an array of size n before even knowing how much n is.
Second mistake is for loop in your main function that goes from 0 to n, i.e. index of an array is out of bounds.
Try this:
int main() {
int n = 0, j = 0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int a[n];
for (j = 0; j < n; j++) {
printf("a[%d] = ", j + 1);
scanf("%d", &a[j]);
}
position_smallest(a,n);
}
If this solved your problem, please mark it.
First of all:
int n, j;
both uninitialized. Initialize them otherwise you will get garbage values.
int n = 0, j = 0;
What happens if n by chance (very likely) is 0 in following line?
int a[n];
You allocate 0 bytes for array a[]. You then enter 10 in following line
scanf("%d", &n);
You will get segmentation fault in for() loop below because your loop is trying to put 10 elements where you allocated no memory at all.
What happens if uninitialized n by chance (very likely) is 2^32 * 4 bytes (ints max)?
int a[n];
You allocate 2^32 bytes for array a[]. You then enter 10 in following line
scanf("%d", &n);
You will not get segmentation fault but you will allocate 2^32 * 4 bytes of memory for your program and you will use only 10
Second if that is not enough:
for (j = 0; j <= n; ++j)
scanf("%d", arr[n];
will access 11th element of array which is undefined behavior, and you might even get segmentation fault there. As you know arrays in C arrays are indexed from 0 to n - 1 (if n is size of array).
Third your loop inside function:
for(i=0; i<=n-1; i=i+1)
is the same as:
for(i=0; i < n; ++i)
And finally, you have a bug in your code, I believe:
if(a[i]<a[0])
should be:
if (a[i] < smallest)
because it is most likely that you would like compare other number to already smallest element not to a[0]. Here is my code
#include <stdio.h>
int position_smallest(int a[],int n) {
int smallest = a[0];
int i = 0, k=0;
for(i=0; i<n; ++i) {
if(a[i]<smallest) {
smallest = a[i];
k = i;
}
}
printf("The smallest value is %d\n", smallest);
printf("It's position is %d\n", k);
return 0;
}
int main() {
int n=0, j=0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int a[n];
for(j=0; j<n; ++j) {
printf("a[%d] = ", j);
scanf("%d", &a[j]);
}
position_smallest(a,n);
return 0;
}
The version above is legit for C99 and up standards. If you are using C89 and earlier compilers you are stack with fixed size as mentioned in #Saurabh's answer or preferably use malloc().
Here is malloc() version:
#include <stdio.h>
#include <stdlib.h>
int position_smallest(int a[],int n) {
int smallest = a[0];
int i=0,k=0;
for(i=0; i<=n-1; i=i+1) {
if(a[i]<smallest) {
smallest = a[i];
k = i;
}
}
printf("The smallest value is %d\n", smallest);
printf("It's position is %d\n", k);
return 0;
}
int main() {
int n=0, j=0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int *a = malloc(sizeof(int) * n);
for(j=0; j<n; j=j+1) {
printf("a[%d] = ", j);
scanf("%d", &a[j]);
}
position_smallest(a,n);
return 0;
}
Use this one:
#include <stdio.h>
int position_smallest(int a[],int n)
{
int smallest = a[0];
int i,k;
for(i=0; i<=n-1; i=i+1)
{
if(a[i]<a[0])
{
smallest = a[i];
k = i;
}
}
printf("The smallest value is %d\n", smallest);
printf("It's position is %d\n", k);
return 0;
}
int main()
{
int n,j;
int a[100];
printf("Enter the size of the array: ");
scanf("%d", &n);
for(j=0; j<n; j=j+1)
{
printf("a[%d] = ", j);
scanf("%d", &a[j]);
}
position_smallest(a,n);
}
Specify the size of the array anything but run the loop according to user's input.
And there is some problems in your function position_smallest. So if you want correct it then take a look at #Gox's answer.
Related
So I need to write a program which takes (by user input) a integer array of 100 elements and finds the max of every 5 elements and writes them in a new array and adds the rest of the numbers to another array.
So an example would be:
original array (array1): 1,23,6,7,16,19,24,56,99,43 ...
soo it will take the first 5 elements: 1,23,6,7,16 and find the max value (23) and add it to the new array
then the next 5 : 19,24,56,99,43 find their max (99) and add it to the other array and so on until it reaches the last number.
second array (array2): 23,99
third array (array3): 1,6,7,16,19,24,56,43
I've tried everything, but i can only get either the Max number in the WHOLE array or the max number of the FIRST 5 elements.
This is how my program should look like:
#include <stdio.h>
#include <stdlib.h>
int array1[100];
int array2[100];
int size;
int main() {
printf("Enter the size of the array: ");
scanf("%d", &size);
if (size > 100) {
return 0;
}
printf("\nEnter %d numbers: \n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array1[i]);
}
printf("\nEntered array: ");
for (int i = 0; i < size; i++) {
printf("%d, ", array1[i]);
}
printf("\n");
// the code for the max numbers should be here
printf("The max values of every 5 elements are: \n");
for(int i = 0; i < size2; i++) {
printf("%d, ", array2[i]);
}
}
"size2" is the size of the new array.
#include <stdio.h>
#include <stdlib.h>
int array1[100];
int array2[100];
int array3[100];
int size, size2, size3;
int main() {
printf("Enter the size of the array: ");
scanf("%d", &size);
if (size > 100) {
return 0;
}
printf("\nEnter %d numbers: \n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array1[i]);
}
printf("\nEntered array: ");
for (int i = 0; i < size; i++) {
printf("%d", array1[i]);
if(i%5==4) printf("| ");
else printf(", ");
}
printf("\n");
// the code for the max numbers should be here
size2=0; size3=0;
int maxvalue_index=0;
for(int i = 1; i < size; i++) {
if(array1[i]>array1[maxvalue_index]) maxvalue_index=i;
if(i%5==4 || i==size-1){
array2[size2]=array1[maxvalue_index];
size2++;
for(int j=i-(i%5); j<=i ; j++){
if(array1[j]==array1[maxvalue_index]) continue;
array3[size3]=array1[j];
size3++;
}
maxvalue_index=i+1;
}
}
printf("The max values of every 5 elements are: \n");
for(int i=0;i<size2;i++) printf("%d ",array2[i]);
printf("\nArray3\n");
for(int i=0;i<size3;i++) printf("%d ",array3[i]);
printf("\n");
}
Try this, It will work for non 5*n lengths
Let me know if there is any error :)
The function below finds the maximum of elements in the array from start to end exclusive.
int findmax(int *const arr, const size_t len, const size_t begin, size_t end) {
int res = arr[begin];
end = (len < end) ? len : end;
for (int i = begin+1; i < end; i++) {
if (arr[i] > res)
res = arr[i];
}
return res;
}
Define this function before your main, then call it for every five elements; the maximum of the first five elements is findmax(array1, size1, 0, 5), for the next
five it would be findmax(array1, size1, 5, 10) and so on and so forth. It shouldn't take that much effort to make a for loop that automates the process.
I have to find out if there is any pair of i,j such that array[i]^2 + array[j]^2 == x^2
.
If there are such pairs, I need to print all such (i,j). Otherwise, print “There are no such pairs”.
#include <stdio.h>
int main(){
int size=10, i, x,j;
int Array[size];
printf("What is the value of x:");
scanf("%d",&x);
for(i=0;i<size;i++){
printf("Enter array value :");
scanf("%d",&Array[i]);
}
for(i=0;i<size;){
for(j=i+1;j<size;j++)
if((Array[i]*Array[i])+(Array[j]*Array[j])==x*x) //how do I complete this for loop?
}
return 0;
}
Yo're almost there, why weren't you incrementing the value of i? Keep a counter to count the matched pairs, then print those or if nothing is found print whatever you want.
#include <stdio.h>
int main() {
int size = 10, i, x, j;
int Array[size];
printf("What is the value of x:");
scanf("%d", &x);
for (i = 0; i < size; i++) {
printf("Enter array value :");
scanf("%d", &Array[i]);
}
int counter = 0;
for (i = 0; i < size; i++) {
for (j = i + 1; j < size; j++)
if ((Array[i] * Array[i]) + (Array[j] * Array[j]) == x * x) {
printf("%d %d\n", Array[i], Array[j]);
counter++;
}
}
if (!counter) {
printf("There are no such pairs\n");
}
return 0;
}
I am new to C and I have a problem. I need to reverse the elements of arrays in the following program. Can I get a very simple explanation of what I am doing wrong and how I could go on about fixing it?
Here's the output I get:
Enter number 0: 0
Enter number 1: 1
Enter number 2: 2
Enter number 3: 3
Enter number 4: 4
Enter number 5: 5
Enter number 6: 6
Enter number 7: 7
Element 0 is: 7
Element 1 is: 6
Element 2 is: 5
Element 3 is: 4
Element 4 is: 4
Element 5 is: 5
Element 6 is: 6
Element 7 is: 7
My code:
#include <stdio.h>
void reverse(int a[], int i)
{
int j=7,b;
for (i=0; i<=7; i++)
{
b=a[i];
a[i]=a[j];
a[j]=b;
printf("Element %d is: %d\n", i,a[i]);
j--;
}
}
int main(void)
{
int a[8];
int i;
for(i=0;i<=7;i++)
{
printf("Enter number %d: ",i);
scanf("%d", &a[i]);
}
reverse(a, 8);
return 0;
}
To avoid being misled, write the output after the reversal, not during it:
#include <stdio.h>
void reverse(int a[], int i)
{
int j = 7, b;
for (i = 0; i <= 7; i++)
{
b = a[i];
a[i] = a[j];
a[j] = b;
j--;
}
}
int main(void)
{
int a[8];
int i;
for (i = 0; i <= 7; i++)
{
printf("Enter number %d: ",i);
scanf("%d", &a[i]);
}
reverse(a, 8);
for (i = 0; i <= 7; i++)
{
printf("Element %d is: %d\n", i, a[i]);
}
return 0;
}
Now you’ll notice the array isn’t changing at all. That’s because you’re swapping every element in the array twice: once to its reversed position, then back to its original position. To fix this, only loop over the first half the array, i.e. while i < 4.
It was probably also intended to make the second argument to reverse the length, so you should make use of that instead of hard-coding 7 or 4:
void reverse(int a[], int length)
{
int i, j = length - 1, b;
for (i = 0; i < length / 2; i++)
{
b = a[i];
a[i] = a[j];
a[j] = b;
j--;
}
}
You could also copy over the array in a new one:
#include <stdio.h>
void reverse(int a[], int i)
{
int b[i];
int j;
for(j=0; j<i; j++){
b[j] = a[i-1-j];
printf("Element %d is: %d\n", j, b[j]);
}
}
int main(void)
{
int a[8];
int i;
for(i=0;i<=7;i++)
{
printf("Enter number %d: ",i);
scanf("%d", &a[i]);
}
reverse(a, 8);
return 0;
}
What you want to do it move through only half of the length of your array else you're reversing the array twice.
void reverse(int a[], int len)
{
int i;
int j=len-1,b;
for(i=0;i<len/2;i++)
{
b=a[i];
a[i]=a[j];
a[j]=b;
j--;
}
for(i = 0; i < len; i++) {
printf("Element %d is: %d\n", i,a[i]);
}
}
The Principe is simple. just stand at the middle of the table and reverse the elements as follow. so pose n is lent of the table. then the last element is n-1.
so you ave to reverse position t[0] and t[n-2]
reverse t[1] and t[n-3] and so on...
stop when you rich the middle of the table. try to code your self is better than for me giving you the code.
hope it helped.
#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.
How to return array of elements from a void function in another c file. My assignment is to reverse the array in lab8.c and then use main.c to show the results I have no idea how to reverse the elements or print the results in main. Functions outside of my main cannot use printf or scanf
main.c
#include <stdio.h>
#include "lab8.h"
int main(void) {
int x[100];
int y[100];
int n = 0;
int count, i, product;
printf("Enter the length of both arrays\n");
scanf("%d", &count);
printf("Enter the %i elements of the first array\n", count);
for(i=0; i<count; i++){
scanf("%i", &x[i]);
}
printf("Enter the %i elements of the second array\n", count);
for(i=0; i<count; i++){
scanf("%i", &y[i]);
}
product = inner_product(x, y, count);
printf("Inner product of first array and second: %i\n", product);
printf("Enter the %i elements of the array\n", count);
for(i=0; i<count; i++){
scanf("%i", &n[i]);
}
reverse(n, count);
printf("Reverse of array 1: %i\n", n);
return(0);
}
lab8.c
#include <stdio.h>
#include "lab8.h"
int inner_product(int a[], int b[], int count){
int i;
int result = 0;
for( i=0; i<count; i++){
result = result + (a[i] * b[i]);
}
return result;
}
void reverse(int a[], int count){
int i, r, end = count - 1;
for(i=0; i<count/2; i++)
r = a[i];
a[i] = a[end];
a[end] = r;
end--;
}
There is one mistake in your code, the for loop in the reverse() function has no braces and hence it's just executing r = a[i] count / 2 times.
void reverse(int a[], int count) {
int i, r, end = count - 1;
for (i = 0 ; i < count / 2 ; i++) {
r = a[i];
a[i] = a[end];
a[end] = r;
end--;
}
}
and to print the result in main() just
reverse(x, count);
printf("Reverse of array 1: %i\n", n);
for (i = 0 ; i < count ; ++i)
printf("%d ", x[i]);
printf("\n");
also, do not ignore the return value of scanf() if it doesn't succeed at reading the values your program will invoke UNDEFINED BEHAVIOR, you should learn good practices right from the beginning.