I'm trying to write a function which finds the largest value in a 2d array with row of 4 and col of 4 where the 2d array is filled with user input. I know my main error is with the array in the function but am not sure what it is.
It would be greatly appreciated if someone could find where I went wrong rather than writing a new code. Unless I just went south.
My attempt:
#include <stdio.h>
void largest(int array);
int main() {
int x,y,trash;
int array[4][4];
int row = 4;
int col = 4;
int bigNum,greater;
bigNum=largest(array);
printf("Please enter a value: ")
for(x=0;x<row;x++) {
for(y=0;y<(col);y++) {
scanf("%d",&array[x][y]);
scanf("%d",&trash);
}
}
printf("The largest number in the array is : ",bigNum);
}
void largest(int array[][]) {
for(x=0; x<row; x++) {
for(y=0; y<(col-1); y++) {
if (array[x][y] > array[x][y+1]) {
array[x][y+1] = array[x][y];
}
}
printf("\n")
}
big = array[x][y];
return big;
}
There are a few problems to address here, the first is you can't have int array[][] as a parameter, you need to provide all but the first dimension, int array[][4] would suffice in this limited case.
However, since row and col are local variables in main they aren't visible to your largest functions body, so they'll need to be passed, make the signature
void largest(int row, int col, int array[row][col]);
Since this is a void function, it can't return any value. It will modify the array it's given in place though, so the changes will be visible.
If you're looking for the straight-up log error, the function to find the largest is pretty crazy. A saner implementation would be to just track the largest element you've seen so far:
int largest(int row, in col, int array[row][col]) {
int big = array[0][0]; // assume the first element is the largest
for (int i = 0; i < row, ++i) {
for (int j = 0; j < col; ++j) {
if (array[i][j] > big) big = array[i][j];
}
}
return big;
}
you could then call the function from main as
int bigNum = largest(row, column, array);
You have declared the function largest as void and trying to store the return value of the function, it is not possible, so change it as int data type.
Then the row and the col variable value is not passed to the largest function.
either you should declare it globally or pass it as a argument to the largest function.
And you are trying to get the value for trash, I don't know what is the need for that. you can remove it.
And do the following changes in the largest function:
#include <stdio.h>
int largest(int [4][4]);
int row = 4;
int col = 4;
int main()
{ int x,y,trash;
int array[4][4];
int bigNum,greater;
printf("Please enter a value: ");
for(x=0;x<row;x++)
{
for(y=0;y<col;y++)
{
scanf("%d",&array[x][y]);
//scanf("%d",&trash);
}
}
bigNum=largest(array);
printf("The largest number in the array is : %d\n",bigNum);
return 0;
}
int largest(int array[4][4])
{
int x=0,y=0,big=array[0][0];
for(x=0;x<row;x++)
{
for(y=0;y<col;y++)
{
if (array[x][y]>big)
{
big = array[x][y];
}
}
printf("\n");
}
return big;
}
Try this code!!!!!
You need to get the values in the array from the user first. So you main should be:
int main()
{ int x,y,trash;
int array[4][4];
int row = 4;
int col = 4;
int bigNum,greater;
printf("Please enter a value: ")
for(x=0;x<row;x++)
{
for(y=0;y<(col);y++)
{
scanf("%d",&array[x][y]);
scanf("%d",&trash);
}
}
bigNum=largest(array);
printf("The largest number in the array is : ",bigNum);
return 0; //Because it is "int" main and not "void" main
}
Array is not initialized and you are doing this
bigNum=largest(array);
You should be finding the largest when you have elements in your array.
There are many other bugs in this code:
The largest function is not proper and it is returning void
You can't have array[][] as your parameter and it should be array[][4].
OMG when I went through the code I found that there is bug in almost each and every line.
#include <stdio.h>
int largest(int array[][4]);
int main()
{ int x,y;
int array[4][4];
int row = 4;
int col = 4;
int bigNum;
printf("Please enter a value: ");
for(x=0;x<row;x++)
{
for(y=0;y<(col);y++)
{
scanf("%d",&array[x][y]);
}
}
bigNum=largest(array);
printf("The largest number in the array is : %d",bigNum);
}
int largest(int array[][4])
{
int max=0;
int x,y;
max = array[0][0];
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
if (array[x][y]>max)
{
max = array[x][y];
}
}
}
return max;
}
Errors in your code
You cannot pass two dimensional arrays like you are doing.
Your logic to find the largest is not proper, you are comparing at any movement two adjacent elements and if the first is small you are actually making them equal
Variable scoping errors, row, col, x and y are bound to main method and you cannot access them in other method. look at Global Variables to access them anywhere you want within the program file.
Unnecessary scanf for trash and printf in largest method.
#include<stdio.h>
int row = 4;
int col = 4;
int largestElement(int array[][4],int row);
int main()
{
int x,y;
int array[row][col];
int bigNum,greater;
printf("Please enter a value: ");
for(x=0;x< row;x++)
{
for(y=0;y< col;y++)
{
scanf("%d",&array[x][y]);
}
}
bigNum = largestElement(array, sizeof(array) / sizeof(array[0]));
printf("The largest number in the array is : ",bigNum);
}
int largestElement(int array[][4],int row)
{
int x,y;
int big =0;
for(x=0;x<row;x++)
{
for(y=0;y<(col-1);y++)
if (big < array[x][y]) big = array[x][y];
}
return big;
}
You try this code for finding the biggest elements in the Two dimensional array.
#include <stdio.h>
int largest(int [][4],int ,int);
int main() {
int x,y,trash;
int array[4][4];
int row = 4;
int col = 4;
int bigNum,greater;
printf("Please enter a value: ");
for(x=0;x<row;x++) {
for(y=0;y<col;y++) {
scanf("%d",&array[x][y]);
}
}
bigNum=largest(array,row,col);
printf("The largest number in the array is:%d ",bigNum);
return 0;
}
int largest(int array[][4], int row, int col)
{
int x,y,big=array[0][0];
for(x=0; x<row; x++) {
for(y=0; y<col-1; y++) {
if(big<array[x][y])
big=array[x][y];
}
}
return big;
}
Related
#include <stdio.h>
#include <math.h>
void abs_table (double x[], int size);
int main (void) {
int size, index;
printf("Enter number of elements: ");
scanf("%d", &size);
double x[size], element;
for (index = 0; index < size; index++) {
printf("Enter x[%d]: ", index);
scanf("%lf", &element);
x[index] = element;
}
void abs_table (double x[], int size);
return 0;
}
void abs_table (double x[], int size) {
int index;
double y[size];
for (index = 0; index < size; index++) {
y[index] = fabs(x[index]);
printf("%lf\t%lf\n", x, y);
}
}
program to read in array values and display the absolute value of each element using a void function.
program terminates after storing values into array x. void function does not display.
Call abs_table() instead of declaring it in main().
Index x and y in abs_table() print statement. As you don't use y other than to print the current value eliminate it.
(minor) Move main() after function so you don't need the declaration.
(minor) Minimize scope of variables.
(minor, not fixed) If your size argument is before x, then you can document their relationship void abs_table(int size, double x[size]).
(minor. not fixed) prefer unsigned variables (index, size etc).
(not fixed) check if scanf() returns 1 in your code otherwise the values size and x[index] may be undefined.
#include <stdio.h>
#include <math.h>
void abs_table (double x[], int size) {
for (int index = 0; index < size; index++) {
printf("%lf\t%lf\n", x[index], fabs(x[index]));
}
}
int main (void) {
int size;
printf("Enter number of elements: ");
scanf("%d", &size);
double x[size];
for (int index = 0; index < size; index++) {
printf("Enter x[%d]: ", index);
scanf("%lf", &x[index]);
}
abs_table(x, size);
return 0;
}
Here is an example run;
Enter number of elements: 3
Enter x[0]: -1.2
Enter x[1]: 1.3
Enter x[2]: -100
-1.200000 1.200000
1.300000 1.300000
-100.000000 100.000000
as a homework assignment, i need to scan N matrices and a user input integer, and scan if any of the matrices values contains that number
without using pointers.
as soon as i finish scanning the array and exits the function, the content of the array resets to zero, or trash if i dont init the array.
#pragma warning(disable:4996)
#include<stdio.h>
#define N 2
int exist(int matrix[][N], int elem);
void input_matrix(int matrix[][N], int size);
void main()
{
int matrix_1[][N] = { 0 }, matrix_2[][N] = { 0 }, matrix_3[][N] = { 0 };
int elem;
printf("please enter values of squared matrix:\n");
input_matrix(matrix_1[][N], N);
//(input_matrix(&matrix_2[N][N]));
// (input_matrix(&matrix_3[N][N]));
printf("please enter number to search for in the matrix:\n");
scanf("%d", &elem);
if (exist(matrix_1,elem))
//printf("exist.");//the next part of h.w to be written when input func works
}
void input_matrix(int matrix[][N], int size)//something here fishy
{
int i, j;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
scanf("%d", &matrix[i][j]);
}
}
}
int exist(int matrix[][N], int elem)
{
int i, j;
int flag = 0;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
if ((matrix[i][j]) == elem)
{
flag = 1;
break;
}
}
}
return flag;
}
Inside the main function, in the call input_matrix(matrix_1[][N], N) you pass the invalid parameter. Instead should pass the whole matrix, like input_matrix(matrix_1, N).
As noted in a comment, it will be better to declare matrix like matrix_1[N][N].
I am writing a program that creates an array of ten integers. I have to define max 10 constant and use functions.getdata(ask user for numbers),displaydata(display)
displaylargest,smallest,average,range,and median.
I am stuck on average because soon as I added that function my largest displays a weird number but if I comment out the average function my largest displays correct answer. Can someone tell me where I went wrong?
#include <stdio.h>
#define MAX 10
int getdata(int array[]);
int displaylargest(int array[]);
int displaysmallest(int array[]);
int displayaverage(int array[]);
void displaydata(int array[]);
int main () {
int array[MAX];
int largest;
int smallest;
int average;
printf("\nEnter ten numbers \n\n");
getdata(array);
displaydata(array );
largest=displaylargest( array);
printf("\nThe largest %d\n", largest);
smallest=displaysmallest( array);
printf("\nThe smallest is %d\n", smallest);
average=displayaverage(array);
printf("\nThe average is %d\n", average);
return 0;
}
int getdata(int array[]) {
int x;
printf ("Enter a number\n ",x+1);
for(x=0;x<MAX;x++)
scanf ("%d",&array[x]);
}
int displaylargest(int array[]) {
int x, largest=array[x];
for (x=0; x<MAX; x++) {
if (array[x]>largest)
largest=array[x];
}
return(largest);
}
int displaysmallest(int array[]) {
int x, smallest=array[x];
for (x=0; x<MAX; x++) {
if (array[x]<smallest)
smallest=array[x];
}
return(smallest);
}
int displayaverage(int array[]) {
int x;
int sum=0;
int average;
for (x=0; x<MAX; x++) {
sum+=array[x];
}
{
average=sum/MAX;
}
return(average);
}
void displaydata(int array[]) {
int x;
for(x=0; x<MAX; x++) {
printf("%d, ",array[x]);
}
}
You need to initialize local variables in your functions especially when you are using that to access your array. Without that local variable can contain any value ,if you use that as index to your array, you might be accessing valid memory.
#include <stdio.h>
#define MAX 10
void getdata(int array[]);
int displaylargest(int array[]);
int displaysmallest(int array[]);
int displayaverage(int array[]);
void displaydata(int array[]);
int main() {
int array[MAX];
int largest;
int smallest;
int average;
printf("\nEnter ten numbers \n\n");
getdata(array);
displaydata(array);
largest = displaylargest(array);
printf("\nThe largest %d\n", largest);
smallest = displaysmallest(array);
printf("\nThe smallest is %d\n", smallest);
average = displayaverage(array);
printf("\nThe average is %d\n", average);
return 0;
}
void getdata(int array[]) {
int x;
printf("Enter a number\n " );
for (x = 0; x<MAX; x++) {
scanf("%d", &array[x]);
}
}
int displaylargest(int array[]) {
int x, largest = array[0];
for (x = 0; x<MAX; x++) {
if (array[x]>largest)
largest = array[x];
}
return(largest);
}
int displaysmallest(int array[]) {
int x, smallest = array[0];
for (x = 0; x<MAX; x++) {
if (array[x]<smallest)
smallest = array[x];
}
return(smallest);
}
int displayaverage(int array[]) {
int x;
int sum = 0;
int average;
for (x = 0; x<MAX; x++) {
sum += array[x];
}
{
average = sum / MAX;
}
return(average);
}
void displaydata(int array[]) {
int x;
for (x = 0; x<MAX; x++) {
printf("%d, ", array[x]);
}
}
In c if we have local variable without initialization ,then it will have some garbage value. And in many of the functions u are intializing somevalue = array(x) it is not correct. The location you are accessing on the array is not valid.Make those chages it will work correctlu
Local variables need to be used here. While defining
largest=array[x];
make sure that x=0, else x may contain garbage value and the array may try to access invalid memory locations, leading to either the conditions known as underflow or overflow.
Also, I see that you've declared getdata() of type int, though it is not returning any value. Please make it of type void, as is just used to insert the asked values into the array. Hope this helps!
Calculating average three by three elements and replacing those elements with the average result.
Example array [1,2,7,-2,5,0, 2,8]
After transformation [3,3,3,1,1,1,5,5]
Something is wrong, I can't get it to work.
#include <stdio.h>
int main ( ) {
int n, c[n];
int *avg;
int pom=0;
printf("Enter lenght of array\n");
scanf("%d",&n);
printf("Enter elements");
for(i = 0;i < n; i++)
scanf("%d",c[i]);
avg=Average(c , n, pom);
for(i = 0; i < n; i++)
printf("Avg elements= %d",*(avg+i))
return 0;
}
int Average(int arr[], int size, int z)
{
int k, l, m, Asum;
if (size < 0) {
return arr;
} else {
k=arr[z];
l=arr[z+1];
m=arr[z+2];
Asum=(k + l + m)/3;
arr[z]=Asum;
arr[z+1]=Asum;
arr[z+2]=Asum;
}
return Average(arr,size--,z++);
}
int n, c[n]; is a problem. n is uninitialized so the size of the array is who-knows-what? This is undefined behavior.
Instead
int main(void) {
int n;
int *avg;
int pom=0;
printf("Enter length of array\n");
if (scanf("%d",&n) != 1) return -1;
int c[n];
for(i = 0;i < n; i++)
// scanf("%d",c[i]);
scanf("%d",&c[i]); // pass the address of an `int`
Likely other issues too.
Try simple input first, imagine what happens when you enter only 1 number, what will the Average function do? Don't run the code but try to execute it in your head or with pencil and paper. If you think the program only has to work with three or more numbers, try three.
A serious program would explicitly reject invalid input.
I am trying to run this code on Dev C++ but it keeps on crashing after the user inputs the two numbers. The program takes input m and n from user two numbers and then returns the output as the solution of the function A which is:
A(m,n) = A(m,n-1)+ A(m-1, n) , if m,n >0
A(m,n) = m-n if m or n <0
Can anybody please tell me why is it happening?
#include<stdio.h>
#include<stdlib.h>
int main() {
int num1=0;
int num2=0;
int rows=0;
int columns=0;
int i,j,**array;
printf("Enter two non-negative integer numbers \n");
scanf("%d %d",&num1,&num2);
//create 2d-Array
rows=num1+1;
columns=num2+1;
array=malloc(rows * sizeof(int *));
for(i=0;i<rows;i++)
{
array[i]=malloc(columns*sizeof(int));
}
//Fill data in array
computeArray(array,rows,columns);
// Display contents of array
for (i = 0; i < rows; i++ )
{
for(j= 0; j < columns; j++ )
{
printf("array[%d][%d] = %d\n", i,j, array[i][j] );
}
}
getch();
return 0;
}
int computeArray (int **array, int rows, int columns) {
int i,j;
for(i=0; i<rows;i++)
{
for(j=0;j<columns;j++)
{
array[i][j]=computeFunction(array,i,j);
}
}
return **array;
}
int computeFunction(int **array, int i, int j) {
int value=0;
if((i<0)||(j <0))
{
value = i-j;
printf("%d",value);
return value;
}
else
{
value = (array[i][j-1] + array[i-1][j]);
printf("%d",value);
return value;
}
return value;
}
When program's behavior is undefined, anything could happen. You should declare a prototype for the function computeArray and computeFunction before main:
int computeArray (int **array, int rows, int columns);
int computeFunction(int **array, int i, int j);
and change
if((i<0)||(j <0)) {...}
in computeFunction to
if((i<=0) || (j <= 0)){...}
&& instead of || may help.
The code fails at the
value = (array[i][j-1] + array[i-1][j]);
line, when j==0.
Debuggers tend to be very useful for spotting simple mistakes. Use them.