So, my task is to let user fill one dimensional array A. This array has M number of groups and P members in each group. I need to fill array B with maximum values of each group in A array and display results of maximum values in each group after. I couldn't figure out any way to do it. I would really appreciate your help since I am a beginner programmer and doing my best to study. So my main problem in code is fillBArray function.
#include <stdio.h>
#include <stdlib.h>
int checkingvariable(int k, int a, int b);
void fillArray(int M, int P,int A[]);
void printArray(int M, int P,int A[]);
void fillBArray(int M, int P,int A[]);
int main()
{
int M, P;
printf("Enter M (the number of groups): ");
scanf("%d", &M);
M=checkingvariable(M, 1, 10);
printf("Enter P (the number of cars in one group): ");
scanf("%d", &P);
P=checkingvariable(P, 1, 10);
int i = P*M;
int A[i];
fillArray(M, P, A);
printArray(M, P, A);
fillBArray(M, P, A);
return 0;
}
void printArray(int M, int P,int A[])
{
int i;
for (i=0 ; i< M*P ; i++)
{
printf("%d ", A[i]);
printf("\n");
}
}
void fillArray(int M, int P, int A[])
{
int i;
for (i=0 ; i< M*P ; i++)
{
printf("Enter speed of car %d: ", i+1);
scanf("%d", &A[i]);
}
}
void fillBArray(int M, int P, int A[])
{
int c, k=0;
int maximum = A[0];
int B[M], group;
for (c = 0; c < P*M; c++)
{
if (A[c] > maximum)
{
maximum = A[c];
}
maximum = B[k];
printf("Maximum value for %d group is: %d", group, maximum);
}
}
int checkingvariable(int k, int a, int b)
{
if (k<a || k>b)
{
while(k<a || k>b)
{
printf("Enter correct value between %d and %d: ", a, b);
scanf("%d", &k);
}
}
return k;
}
One way is to replace one loop with two nested loops. The outer loop would iterate groups, while the nested loop would iterate members in each group.
One observation before you begin: members of a group g in the array A are located between indexes g*P, inclusive, and (g+1)*P, exclusive. Member m of a group is located at the index A[g*P + m].
Now it should be clear how to make your loops:
for (int g = 0 ; g != M ; g++) { // Groups
int max = A[g*P];
for (int m = 1 ; m != P ; m++) { // Members
... // Compute max
}
// Store max for group g in B[]
}
#include <stdio.h>
#include <stdlib.h>
int checkingvariable(int k, int a, int b);
void fillArray(int M, int P,int A[]);
void printArray(int M, int P,int A[]);
void fillBArray(int M, int P,int A[]);
int main()
{
int M, P;
printf("Enter M (the number of groups): ");
scanf("%d", &M);
M=checkingvariable(M, 1, 10);
printf("Enter P (the number of cars in one group): ");
scanf("%d", &P);
P=checkingvariable(P, 1, 10);
int i = P*M;
int A[i];
fillArray(M, P, A);
printArray(M, P, A);
fillBArray(M, P, A);
return 0;
}
void printArray(int M, int P,int A[])
{
int i;
for (i=0 ; i< M*P ; i++)
{
printf("%d ", A[i]);
printf("\n");
}
}
void fillArray(int M, int P, int A[])
{
int i;
for (i=0 ; i< M*P ; i++)
{
printf("Enter speed of car %d: ", i+1);
scanf("%d", &A[i]);
}
}
/*void fillBArray(int M, int P, int A[])
{
int c, k=0;
int maximum = A[0];
int B[M], group;
for (c = 0; c < P; c++)
{
if (A[c] > maximum)
{
maximum = A[c];
}
if (c < P)
{
group = 1;
maximum = A[c];
printf("Maximum value for %d group is: %d", group, maximum);
}
if (c < P*2)
{
group = 2;
k=1;
maximum = A[c];
printf("Maximum value for %d group is: %d", group, maximum);
}
}
}*/
void fillBArray(int M, int P, int A[])
{
int B[M], maximum = 0, m;
for (int g = 0 ; g != M ; g++)// Groups
{
int max = A[g*P];
for (m = 0 ; m != P ; m++) // Members
{
if (A[g*P + m] > maximum)
{
maximum = A[g*P + m];
}
}
B[g] = maximum;
printf("Maximum value for %d group is: %d", g+1, maximum);
printf("\n");
maximum = 0;
}
}
int checkingvariable(int k, int a, int b)
{
if (k<a || k>b)
{
while(k<a || k>b)
{
printf("Enter correct value between %d and %d: ", a, b);
scanf("%d", &k);
}
}
return k;
}
This is answer to my problem, check fillBArray.
Related
#include<stdio.h>
void printarr( int arr , int a,int b){
for(int z=0;z<a;z++){
for(int x=0;x<b;x++){
printf("the marks of student %d in subject %d and %d is :%d\n",z+1 ,
x+1 , x+2 , arr);
}
}
}
int main(){
int n_students = 5;
int n_sub = 2;
int marks[5][2];
for (int i=0; i<n_students; i++){
for (int j=0; j<n_sub; j++){
printf("enter the marks of student %d in subject %d\n", i+1, j+1);
scanf("%d", marks[i][j]);
}
}
printarr(marks , 5 , 2);
return 0;
i am getting to put only two times and the outer loop is not repeating itself
please explain me in simple terms , i am just learning this launguage
complete begineer.
1.When passing two-dimensional arrays, it is not mandatory to specify the number of rows in the array. However, the number of columns should always be specified.
void printarr( int arr , int a,int b){ --->void printarr( int arr [][2], int
a,int b){
2.When reading array element in scanf you have missed &
scanf("%d", marks[i][j]);--->scanf("%d", &marks[i][j]);
3.When Printing array elements in printf you have to specify the index.
arr ---> arr[i][j]
#include<stdio.h>
void printarr( int arr [][2], int a,int b){
for(int z=0;z<a;z++){
for(int x=0;x<b;x++){
printf("the marks of student %d in subject %d is :%d\n",z+1,x+1,arr[z][x]);
}
}
}
int main(){
int n_students = 5;
int n_sub = 2;
int marks[5][2];
for (int i=0; i<n_students; i++){
for (int j=0; j<n_sub; j++){
printf("enter the marks of student %d in subject %d\n", i+1, j+1);
scanf("%d", &marks[i][j]);
}
}
printarr(marks, 5 , 2);
return 0;
}
You want this: (explanation in comments)
#include <stdio.h>
#include<stdio.h>
void printarr(int a, int b, int arr[a][b]) { // pass an array, not an int,
// and a and b must be before arr
for (int z = 0; z < a; z++) {
for (int x = 0; x < b; x++) {
printf("the marks of student %d in subject %d and %d is :%d\n", z + 1,
x + 1, x + 2, arr[z][x]); // use arr[x][z] here. arr obviously dons' make sense
}
}
}
int main() {
int n_students = 5;
int n_sub = 2;
int marks[5][2];
for (int i = 0; i < n_students; i++) {
for (int j = 0; j < n_sub; j++) {
printf("enter the marks of student %d in subject %d\n", i + 1, j + 1);
scanf("%d", &marks[i][j]); // you forgot the &
}
}
printarr(5, 2, marks); // you need to pass the size beforen the array
return 0; // read the documentation about VLAs
}
i am learning pointers and i write these code to add two martix via function, it compile well and give correct answer for 2x2 matrix but for more than two row and col it fails when i try it with 3x3 matrix [0][0] element for the first matrix it automaticaly become 6 and [1][0] it became 9 every time, i can't figure out why this happen
#include <stdio.h>
//-----------------Function------------------
void add(int (*p1)[10], int (*p2)[10], int (*p3)[10], int r, int c);
void get(int (*p1)[10], int r, int c);
void print(int (*p1)[10], int r, int c);
//---------------Main Program----------------
int main()
{
//declartion
int i, j, r, c;
printf("\n\tenter the row and column of matrix\n\t");
scanf("%d %d", &r, &c);
int m1[r][c], m2[r][c], m3[r][c];
printf("\n\tenter the element of first matrix\n\t");
get(m1, r, c);
print(m1, r, c);
printf("\n\tenter the element of second matrix\n\t");
get(m2, r, c);
print(m2, r, c);
add(m1, m2, m3, r, c);
printf("\n");
print(m3, r, c);
return(0);
}
//-------------Define Function---------------
//get()
void get(int (*p1)[10], int r, int c)
{
int i, j;
for(i = 0; i < r; i++)
{
printf("\n\t");
for(j = 0; j < c; j++)
{
scanf("%d", (*(p1+i)+j));
}
}
}
//--------------------X----------------------
//add()
void add(int (*p1)[10], int (*p2)[10], int (*p3)[10], int r, int c)
{
int i, j;
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
//printf("\n%d %d = %d & %d", i, j, *(*(p1+i)+j), *(*(p2+i)+j));
*(*(p3+i)+j) = *(*(p1+i)+j) + *(*(p2+i)+j);
}
}
}
//--------------------X----------------------
//print()
void print(int (*p1)[10], int r, int c)
{
int i, j;
for(i = 0; i < r; i++)
{
printf("\n");
for (j = 0; j < c; j++)
{
printf("\t%d", *(*(p1+i)+j));
}
}
}
//--------------------X----------------------
Drop the array pointer notation and the fixed size. Use variable-length arrays based on the parameters.
Remove useless comments like // get ... void get...
Don't use unreadable de-referncing with *(arr+i) when you could be writing readable arr[i].
Print new line after each row, not before each row.
Here's a cleaned up program which compiles. I haven't tested it.
#include <stdio.h>
void add (int r, int c, int p1[r][c], int p2[r][c], int p3[r][c]);
void get (int r, int c, int p1[r][c]);
void print (int r, int c, int p1[r][c]);
int main (void)
{
int i, j, r, c;
printf("\n\tenter the row and column of matrix\n\t");
scanf("%d %d", &r, &c);
int m1[r][c], m2[r][c], m3[r][c];
printf("\n\tenter the element of first matrix\n\t");
get(r, c, m1);
print(r, c, m1);
printf("\n\tenter the element of second matrix\n\t");
get(r, c, m2);
print(r, c, m2);
add(r, c, m1, m2, m3);
printf("\n");
print(r, c, m3);
return(0);
}
void get (int r, int c, int p1[r][c])
{
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
scanf("%d", &p1[i][j]);
}
}
}
void add (int r, int c, int p1[r][c], int p2[r][c], int p3[r][c])
{
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
p3[i][j] = p1[i][j] + p2[i][j];
}
}
}
void print (int r, int c, int p1[r][c])
{
for(int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
printf("\t%d", p1[i][j]);
}
printf("\n");
}
}
Problem: Write a program in C to get the largest element, smallest element, sum of all elements and multiplication of all elements of an array using the functions. (Make four different functions for four calculations and call them for one array given by user).
I think I'm getting error because of i and n.
I'm a beginner and I can't explain every thing line by line. Code:
#include <stdio.h>
// defined Max function int Max(int arr[], int);
// defined Min function int Min(int arr[], int);
// defined Sum function int Sum(int arr[], int);
// defined Mul function int Mul(int arr[], int);
int main() {
int i, n, arr[100];
printf("Input the number of elements to be stored in the array : ");
scanf("%d",&n);
printf("Input %d elements in the array: \n",n);
for (i=0; i<n; i++)
{
printf("element - %d : ",i);
scanf("%d", &arr[i]);
}
n = Max( arr, n);
printf("The largest element in the array is : %d", n);
n = Min( arr, n);
printf("\nThe smallest element in the array is : %d", n);
n = Sum( arr, n);
printf("\nThe sum of all the elements in the array is : %d", n);
n = Mul( arr, n);
printf("\nThe multiplication of all the elements in the array is : %d", n);
return 0;
}
int Max(int arr[], int n)
{
int max = arr[0];
for (int i=0; i<n; i++)
{
if (max<arr[i])
max=arr[i];
}
return max;
}
int Min(int arr[], int n)
{
int min = arr[0];
for (int i=0; i<n; i++)
{
if (min>arr[i])
min=arr[i];
}
return min;
}
int Sum(int arr[], int n)
{
int sum = arr[0];
for (int i=0; i<n; i++)
{
sum += arr[i];
}
return sum;
}
int Mul(int arr[], int n)
{
int mul = arr[0];
for (int i=0; i<n; i++)
{
mul *= arr[i];
}
return mul;
}
As suggested in one comment, your for should start from 1, otherwise you use the element 0 twice, and thus producing always a wrong result. Here is how the for related to Mul function should appear, just apply the same for other ones:
int mul = arr[0];
for(int i=1; i<n; i++)
{
mul *= arr[i];
}
return mul;
In addition to the above, there is another bug in your code, which is the way you use the variable n.
As far as I see, that variable stores the number of elements of the array. It is a valid input for all the sub-routines, but you cannot re-assign it with the result of each of them. In this way, it's like you are invoking the sub-routines always with a different array size, and of course you get unexpected results.
So, one possible solution would be to define and use another local variable which holds the results from each sub-routines.
To help you with your issue, I fixed your code, it works fine now. There you go:
#include <stdio.h>
// defined Max function int Max(int arr[], int);
// defined Min function int Min(int arr[], int);
// defined Sum function int Sum(int arr[], int);
// defined Mul function int Mul(int arr[], int);
int main() {
int i, n, op_res, arr[100];
printf("Input the number of elements to be stored in the array : ");
scanf("%d",&n);
printf("Input %d elements in the array: \n",n);
for (i=0; i<n; i++)
{
printf("element - %d : ",i);
scanf("%d", &arr[i]);
}
op_res = Max( arr, n);
printf("The largest element in the array is : %d", op_res);
op_res = Min( arr, n);
printf("\nThe smallest element in the array is : %d", op_res);
op_res = Sum( arr, n);
printf("\nThe sum of all the elements in the array is : %d", op_res);
op_res = Mul( arr, n);
printf("\nThe multiplication of all the elements in the array is : %d", op_res);
return 0;
}
int Max(int arr[], int n)
{
int max = arr[0];
for (int i=0; i<n; i++)
{
if (max<arr[i])
max=arr[i];
}
return max;
}
int Min(int arr[], int n)
{
int min = arr[0];
for (int i=0; i<n; i++)
{
if (min>arr[i])
min=arr[i];
}
return min;
}
int Sum(int arr[], int n)
{
int sum = arr[0];
for (int i=1; i<n; i++)
{
sum += arr[i];
}
return sum;
}
int Mul(int arr[], int n)
{
int mul = arr[0];
for (int i=1; i<n; i++)
{
mul *= arr[i];
}
return mul;
}
I tried to build a heap and finally print the elements in the form of an array.
Here it is the code (I know this doesn't really make sense but I just wanted to test my knowlwdge of heap and dynamic arrays):
#include <stdio.h>
#include <stdlib.h>
void heapiify(int *arr,int n, int i)
{
int largest=i;
int l=2*i+1; // left node
int r= 2*i+2; // right node
if(l<=n && *arr[l]>=*arr[i])
largest=l;
if (r <=n && *arr[r]<=*arr[i])
largest= r;
if(largest !=i)
{
int temp=*arr[i];
*arr[i]=*arr[largest];
*arr[largest]=temp;
}
heapify(*arr,n,largest);
}
void buildh(int *arr,int n,int r,int c)
{
int i;
for(i=n/2-1;i>=0;i--)
heapify(*arr,n,i);
output(*arr,r,c);
}
void output(int *arr,int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d",*arr[i*c+j]);
}
printf("\n");
}
}
int main()
{
int i,j,r,c;
printf("enter the number of rows");
scanf("%d",&r);
printf("enter the number of columns");
scanf("%d",&c);
int n=r*c;
int *arr=malloc(n*sizeof(int));
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
scanf("%d",&arr[i*c+j]);
}
buildh(*arr,n,r,c);
}
I'm getting 9 errors which are all the same
invalid argument type of unary '*'( have int)
Your arr variable is of type pointer to int:
int *arr=malloc(n*sizeof(int));
So when you call buildh, which takes the same type, you have to pass it as-is:
buildh(arr,n,r,c);
Same for the other cases.
The problem is the dereference of arr, across your funtions in multiple places, and the passing of dereferenced *arr in your functions to int * parameters, you should pass arr, try:
//...
void heapify(int *arr, int n, int i)
{
int largest = i;
int l = 2 * i + 1; // left node
int r = 2 * i + 2; // right node
if (l <= n && arr[l] >= arr[i]) //here
largest = l;
if (r <= n && arr[r] <= arr[i]) //here
largest = r;
if (largest != i)
{
int temp = arr[i]; //here
arr[i] = arr[largest]; //here
arr[largest] = temp; //here
}
heapify(arr, n, largest); //here
}
void buildh(int *arr, int n, int r, int c)
{
int i;
for (i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i); //here
output(arr, r, c); //here
}
void output(int *arr, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d", arr[i * c + j]); //here
}
printf("\n");
}
}
int main()
{
//...
buildh(arr, n, r, c); //here
}
Here's my problem :
I have a program where the user have to fill this :
a 2d array of a list of elements from which i can at most take one element ( we'll call it exclusionArr )
a 2d array of a list of elements from which i have to at least take one element ( we'll call it inclusionArr )
an array with the elements i have to keep ( we'll call it keepArr )
an array with the elements i can't use ( we'll call it throwArr )
Now on the first step i need to check if any row of exclusionArr contain an element of keepArr and if it does then i need to add every element of that row but that one in throwArr.
One row cannot contain more than one element of keepArr, i'll make a function to check for that and return an error if it is. ( you can at most keep one item from the rows of exclusionArr so having 2 elements of keepArr on the same row is a problem )
I need help to add element to an array, i can't seem to be able to store those values in my array (more like i don't really know how to, still new to C).
Here's the function i made so far :
void interdiction(int *throwArr[], int *throw_size, int keepArr[], int keep_size, int x, int y, int exceptionArr[x][y]) {
int i, j, k, count=0;
while(count<keep_size) {
for(i=0;i<x;i++) {
for(j=0;j<y;j++) {
if (exceptionArr[i][j] == keepArr[count]) {
for (k=0;k<y;k++) {
if(k!=j) {
printf("\nElement %d of exclusion %d inserted in throwArr",exceptionArr[i][k], i);
*throw_size+=1;
throwArr[*throw_size]=exceptionArr[i][k];
}
else printf("\nelement to keep found in exclusion %d in position %d", i, j);
}
}
}
}
count++;
}
}
I would like it to change the throwArr that i put in the function so that it adds on the already existing array every element on the current row except the element that's in keepArr.
I don't know if it's relevant or not but for throwArr when initializing it i allocate a lot of extra memory so i could perform changes without running out of space so i don't know if i need to realloc memory in the function for the changes done.
Any help would be greatly appreciated !
EDIT : Here's the full code
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void interdiction(int *throwArr, int throw_size, int *keepArr, int keep_size, int x, int y, int exclusionArr[x][y]);
static int compare (void const *a, void const *b);
void noDuplicate( int arr[], int *size );
void xclusion_alloc (int x, int y, int(**aptr)[x][y]);
void xclusion_print (int x, int y, int array[x][y]);
void xclusion_fill (int x, int y, int array[x][y]);
void main(){
int throw_size, keep_size, rexc, lexc;
int i, j;
int nbObjets=7, *throwArr, *keepArr, (*exclusionArr)[rexc][lexc];
printf("\nHow many exclusions :");
scanf("%d", &rexc);
printf("\nHow many elements in each exclusion :");
scanf("%d", &lexc);
xclusion_alloc(rexc,lexc,&exclusionArr);
xclusion_fill(rexc,lexc,*exclusionArr);
xclusion_print(rexc,lexc,*exclusionArr);
printf("\nHow many elements do we have to keep :");
scanf("%d", &keep_size);
keepArr=malloc(nbObjets*sizeof(int));
printf("\nWhat are they :");
for(i=0;i<keep_size;i++){
scanf("%d",&keepArr[i]);
}
qsort(keepArr, keep_size, sizeof *keepArr, compare);
noDuplicate(keepArr, &keep_size);
printf("\nHow many elements we can't use :");
scanf("%d", &throw_size);
throwArr=malloc(nbObjets*sizeof(int));
printf("\nWhat are they :");
for(i=0;i<throw_size;i++){
scanf("%d",&throwArr[i]);
}
qsort(throwArr, throw_size, sizeof *throwArr, compare);
noDuplicate(throwArr, &throw_size);
interdiction(throwArr, throw_size, keepArr, keep_size, rexc, lexc, *exclusionArr);
printf("\nOur array of elements we can't use : ");
for (i=0;i<throw_size;i++){
printf("%d ", throwArr[i]);
}
}
static int compare (void const *a, void const *b){
int const *pa = a;
int const *pb = b;
return *pa - *pb;
}
void interdiction(int *throwArr, int throw_size, int *keepArr, int keep_size, int x, int y, int exclusionArr[x][y]){
int i, j, k, count=0;
while(count<keep_size){
for(i=0;i<x;i++) {
for(j=0;j<y;j++) {
if (exclusionArr[i][j] == keepArr[count]) {
for (k=0;k<y;k++){
if(k!=j){
printf("\nElement %d of exclusion %d inserted in the array of elements we can't use",exclusionArr[i][k], i);
throw_size+=1;
throwArr[throw_size]=exclusionArr[i][k];
}
else printf("\nelement to keep found in exclusion n°%d in position %d", i, j);
}
}
}
}
count++;
}
}
void noDuplicate( int arr[], int *size ) {
int i=0, j=0;
for (i = 1; i < *size; i++) {
if (arr[i] != arr[j]) {
j++;
arr[j] = arr[i];
}
}
*size = (j + 1);
}
void xclusion_alloc (int x, int y, int(**aptr)[x][y]) {
*aptr = malloc( sizeof(int[x][y]) );
assert(*aptr != NULL);
}
void xclusion_fill (int x, int y, int array[x][y]) {
int i, j;
for(i=0; i<x; i++) {
for(j=0; j<y; j++) {
scanf("%d", &array[i][j]);
}
}
}
void xclusion_print (int x, int y, int array[x][y]) {
int i,j;
for(i=0; i<x; i++) {
printf("\nExclusion n°%d :", i);
printf(" { ");
for(j=0; j<y; j++) {
printf("%d ", array[i][j]);
}
printf("}");
printf("\n");
}
}
Sadly the output i'm getting is like this :
How many exclusions :3
How many elements in each exclusion :2
5 3
2 7
4 1
Exclusion n░0 : { 5 3 }
Exclusion n░1 : { 2 7 }
Exclusion n░2 : { 4 1 }
How many elements do we have to keep :1
What are they :5
How many elements we can't use :1 2
What are they :
element to keep found in exclusion n░0 in position 0
Element 3 of exclusion 0 inserted in the array of elements we can't use
Our array of elements we can't use : 2
I managed to make it work by making the following changes to my function :
void interdiction(int **throwArr, int *throw_size, int *keepArr, int keep_size, int x, int y, int exclusionArr[x][y]){
int i, j, k, count=0;
while(count<keep_size){
for(i=0;i<x;i++) {
for(j=0;j<y;j++) {
if (exclusionArr[i][j] == keepArr[count]) {
for (k=0;k<y;k++){
if(k!=j){
printf("\nElement %d of exclusion %d inserted in the array of elements we can't use",exclusionArr[i][k], i);
(*throwArr)[*throw_size]=exclusionArr[i][k];
*throw_size+=1;
}
else printf("\nelement to keep found in exclusion n°%d in position %d", i, j);
}
}
}
}
count++;
}
}
Okay I think you need to change those lines as following. Also you made a mistake when you were testing your program.
void interdiction(int *throwArr, int throw_size, int *keepArr, int *keep_size, int x, int y, int exclusionArr[x][y]);
interdiction(throwArr, throw_size, keepArr, &keep_size, rexc, lexc, *exclusionArr);
void interdiction(int *throwArr, int throw_size, int *keepArr, int *keep_size, int x, int y, int exclusionArr[x][y]) {
...
throwArr[*throw_size]=exclusionArr[i][k]; // these two lines switched order
*throw_size+=1;
...
}