I am writing a code that uses a 2D array to store the house number and the number of children in the house, but I am having little trouble with the for loops and I don't really understand what the problem is. The dimensions of the array are the number of houses being the number of columns (this is a user input), and only two rows as the number of kids is inputted into the second row by the user. the code is shown below:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0;
int j=0;
int houses=0;
int kids=0;
printf("How many houses are there in the street?\n");
scanf("%d", &houses);
int KidsInStreet[houses][2];//Columns are the houses and the rows are
the number of kids
for(i=0;i<houses;i++)
{
for(j=0;j<2;j++)
{
printf("Enter the value for KidsInStreet[%d][%d]:\n", i, j);
scanf("%d", &KidsInStreet[i][j]);
}
}
for (i=0;i<houses;i++)
{
for(j=0;j<2;j++)
{
printf("House number:%d. Number of kid(s):%d\n", KidsInStreet[i]
[j]);
}
}
return 0;
}
If I understood the requirement well you don't need 2 for loops. So your code is the following one:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0;
int j=0;
int houses=0;
int kids=0;
printf("How many houses are there in the street?\n");
scanf("%d", &houses);
int KidsInStreet[houses][2];//Columns are the houses and the rows are the number of kids
for(i = 1; i <= houses; i++)
{
KidsInStreet [i][1] = i;
printf("Enter the value for KidsInStreet[%d]:\n", i);
scanf("%d", &KidsInStreet[i][2]);
}
for (i = 1; i <= houses; i++)
{
printf("House number:%d. Number of kid(s):%d\n", KidsInStreet[i][1], KidsInStreet[i][2]);
}
}
Related
textI have to make one array which is A[n][n]. In this array the user puts some numbers and from this array I have to make a second array which is C[n] which numbers are positive number by row from first one. I try do it by this way but in that way it works for every numbers it have to be only by rows .have to look something like that for example
A00:1 A01:-3 A02:5
A10:-7 A11:-8 A12:7
A20:6 A21:9 A22:10
C00:2 C01:1 C02:3
But in my way it look :
C00:3 C01:3 C02:3 which is wrong.
If somebody have an idea where is the problem I will be so thankful.
> const int n=10;
int a[n][n];
int c[n];
int i,j,k,suma;
printf("vuvedete br redove i stalbove n=");
scanf ("%d",&n);
for (i=0;i<n;i++){
for (j=0;j<n;j++){
do{
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
while(a[i][j]<-1000||a[i][j]>1000);
}
}
suma=0;
for (i=0;i<n;i++){
for (j=0;j<n;j++){
if (a[i][j]>0){
suma=suma+1;}
}
}
for(k=0;k<n;k++){
printf("c[%d]=%d",k,suma);
}
return 0;
}
Main changes:
Populate array c[] and discard variable suma. This was the primary issue with your code.
You declare n as a const but then allow user to enter its value. I've removed the const and used a #define to set the max dimensions for the arrays.
Removed while loop - what's this for?
General tidying up.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX 10
int main() {
int a[MAX][MAX] = {0};
int c[MAX] = {0};
int n = MAX;
printf("vuvedete br redove i stalbove n=");
scanf ("%d",&n);
for (int i=0;i<n;i++)
for (int j=0;j<n;j++) {
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
c[i] += a[i][j] > 0 ? 1 : 0;
for(int k=0;k<n;k++)
printf("c[%d]=%d\n",k,c[k]);
return 0;
}
I have just started learning how to program and I came across an assignment whereby I have to create an array wherein the weight of the elephant seals is read & printed. Can someone please tell me what's wrong with my code, because I can read in the data but my data is being printed wrong when I call the printArray function.
Below is my code:
/*
compute the weight average of elephant seals using an array that will read in the data
*/
#include <stdio.h>
void arrayReadingData(const int sizeP) //array to read in the weight of the elephantseals
{
int weightP[sizeP];
for(int i = 1; i <= sizeP; i++)
{
printf("Elephant seal %d weight is: ", i);
scanf("%d", &weightP[i]);
}
}
void printArray(const int sizeP)
{
int weightP[sizeP];
for(int i = 1; i <= sizeP; i++)
{
printf("%d\t", weightP[i]);
}
}
int main(void)
{
int size;
printf("How many seals do you want to calculate the average weight for: ");
scanf("%d", &size);
arrayReadingData(size);
printArray(size);
};
You are doing the for loop wrong, your loop should start at i = 0 to i < sizeP, when i = sizeP weights[sizeP] will be assigned an arbitrary value.
Update:
I just noticed you are declaring the array as a local variable to each function and declaring it the wrong way, you should declare that array in the main function and pass it to your functions instead
Here is a solution you can use
/*
compute the weight average of elephant seals using an array that will read in the data
*/
#include <stdio.h>
#define SIZE 3
void arrayReadingData(int weightP[]) //array to read in the weight of the elephantseals
{
for(int i = 0; i < SIZE; i++)
{
printf("Elephant seal %d weight is: ", i);
scanf("%d", &weightP[i]);
}
}
void printArray(int weightP[])
{
for(int i = 0; i < SIZE; i++)
{
printf("%d\t", weightP[i]);
}
}
int main(void)
{
int weights[SIZE];
arrayReadingData(weights);
printArray(weights);
};
Note: if you want the size to be given by the user you can use pointers and allocate memory dynamically
Here is how you can achieve that:
/*
compute the weight average of elephant seals using an array that will read in the data
*/
#include <stdio.h>
#include <stdlib.h>
void arrayReadingData(int weightP[], int size) //array to read in the weight of the elephantseals
{
for(int i = 0; i < size; i++)
{
printf("Elephant seal %d weight is: \n", i);
scanf("%d", &weightP[i]);
}
}
void printArray(int weightP[], int size)
{
for(int i = 0; i < size; i++)
{
printf("%d\t", weightP[i]);
}
}
int main(void)
{
int* weightsArray;
int size;
printf("Enter the size of the array:\n");
scanf("%d", &size);
weightsArray = (int*)malloc(size*sizeof(int));
if(weightsArray == NULL){
printf("couldn't allocate memory");
exit(1);
}
arrayReadingData(weightsArray, size);
printArray(weightsArray, size);
free(weightsArray);
};
I redid the code, after learning from the comments and I came up with this solution, the problem is that now I can only read in 10 numbers and print 10 numbers if I read in 11 numbers and try to print it I get an error stating zsh: segmentation fault
/*
compute the weight average of elephant seals using an array that will read in the data
*/
#include <stdio.h>
void arrayReadingData(const int sizeP, int weightP[]) //array to read in the weight of the elephant seals
{
for(int i = 0; i < sizeP; i++)
{
printf("Elephant seal %d weight is: ", i);
scanf("%d", &weightP[i]);
}
}
void printArray(const int sizeP, int weightP[])
{
for(int i = 0; i < sizeP; i++)
{
printf("%d\n", weightP[i]);
}
}
int main(void)
{
int size;
int weight[size];
printf("How many seals do you want to calculate theaverage weightfor:");
scanf("%d", &size);
arrayReadingData(size, weight);
printArray(size, weight);
}
The Question is to take dimension of array and elements of it from the user. Then calculate the sum of each column in array, it stops midway for some reason. I am new to C, is there a problem in usig malloc not in right way?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m,n;
int arr[m][n];
int **arr;
int i,j;
printf("Enter Number of Rows and Columns:\t");
scanf("%d %d", &m, &n);
arr=(int**)malloc(sizeof(int*)*m);
for(i=0;i<10;i++)
{
arr[i]=(int*)malloc(sizeof(int)*n);
}
printf("Type Array Elements:\t");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", &arr[i][j]);
}
}
printf("This is Where It Stops");
//ADDITION OF COLUMNS
int Sum;
int *sum=(int*)malloc(j*sizeof(int));
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
{
Sum=0;
Sum=Sum+arr[i][j];
}
sum[j]=Sum;
}
for(j<0;j<0;j++)
{
printf("Sum of Column %d is %d", j, sum[j]);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int a[20],int n);
int main()
{
int a[20];
int n;
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the array elements\n");
for(int i=0;i<n;i++)
{
scanf("%d ",&a[i]);
}
reverse(a,n);
return 0;
}
int reverse(int a[20],int n)
{
for(int i=n-1;i>=0;i--)
{
printf("%d ",a[i]);
}
return 0;
}
here if I input n=4 then during runtime i have to take 5 elements and then it reverses.For eg if i take n=4 and then for no of elements i have to take 1,2,3,4,5 and then only output is coming as 4 3 2 1.Why? is my logic wrong? also in this code I am unable to take the number of elements of arrays in a straight line, like 1 2 3 4.When I am entering the number each number is entering in new line .I am a novice programmer in C and thus having these doubts.Please anyone explain...
The problem with your code is the extra space after %d in your scanf line where you accept array elements i.e.
for(int i=0;i<n;i++)
{
scanf("%d ",&a[i]); //should be scanf("%d",&a[i]);
}
Change that and you're good to go.
Here is your entire program refactored to work correctly:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int a[20], int n)
int main()
{
int a[20];
int n;
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the array elements\n");
for (int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
reverse(a,n);
return 0;
}
int reverse(int a[20], int n)
{
int mid = n/2;
for (int i=0; i < mid; ++i)
{
int temp = a[n-i-1];
a[n-i-1] = a[i];
a[i] = temp;
}
return 0;
}
How can I get each element of an array from user input then pass it to a function in another file. I'm having trouble and need help please. This my code.
main.c
#include <stdio.h>
#include "lab8.h"
int x[100];
int y[100];
int main(void) {
int count, i, product;
printf("Enter the length of both arrays\n");
scanf("%d", &count);
printf("Enter the first array's elements\n");
for(i=0; i<count; i++){
scanf("%i", &x[i]);
}
printf("Element: %i\n", x[i]);
printf("Enter the second array's elements\n");
for(i=0; i<count; i++){
scanf("%i", &y[i]);
}
product = inner_product(x, y, count);
printf("Inner product: %i\n", product);
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=1; i<count; i++) {
result = result + (a[i] * b[i]);
}
return result;
}
It only seems to multiply the last element entered for both arrays heres the output.
Enter the length of both arrays
2
Enter the first array's elements
1
2
Element: 0
Enter the second array's elements
3
3
Inner product: 6
The problem is that you are iterating from 1 and you should iterate from 0, in the inner_product() function
for( i=1; i<count; i++) {
/* ^ this should be 0 */
also, don't use global variables specially because you got the rest of it right, you are passing the arrays as arguments to the inner_product() function.