Understanding the array and loop answer - c

Can you go through all the steps as why is the answer is 9, 0 and 3.
This prints 9. How?
int sum,i, j, g[3][3] = {{0,0,0},{1,1,1},{2,2,2}};
sum = 0;
for (i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
sum=sum+g[i][j];
}
printf("The value is:%d", sum);
This prints 0. How?
sum = 1;
for (i=0;i<=2;i++)
{
for(j=0;j<=1;j++)
sum=sum*g[i][j];
}
printf("The value is:%d", sum);`
This prints 3. How?
sum = 0;
for (i=0;i<=2;i++)
sum=sum+g[i][1];
printf("The value is:%d", sum);

To see how it works try something like:
int sum,i, j, g[3][3] = {{0,0,0},{1,1,1},{2,2,2}};
sum = 0;
for (i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
sum=sum+g[i][j];
printf("i=%d, j=%d, sum=%d", i, j, sum);
}
}
printf("The value is:%d", sum);

Try this...
int sum,i, j, g[3][3] = {{0,0,0},{1,1,1},{2,2,2}};
sum = 0;
for (i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
sum=sum+g[i][j];
printf("i=%d, j=%d, g[%d][%d]=%d, sum=%d", i, j, i, j, g[i][j], sum);
}
}
printf("The value is:%d", sum);

Related

How do I count the power of a number in my for loop in C?

I'm created a program that does prime factorization. However, I need the output to contain power of prime numbers so instead of the output being: 2.2.2.3, it should be 2^3.3
The only problem is that the exponents aren't what they should be. I don't know where the mistake is.
Here's my for loop:
#include <stdio.h>
int main(){
int num, val, i, prime=0, check, check2, count=0, counter=1;
while(num != 0){
printf("\nInput number: ");
scanf("%d", &num);
val=num;
for(i=2; i<=num; i++){
if(num%i==0){
count++;
if(count==1 && val!=i){
check=i;
}
else if(val!=i){
check2=i;
}
else{
prime=1;
printf("%d is prime", val);
}
num/=i;
if(check==check2){
counter++;
}
if(prime==0){
if(count==1){
if(counter==1)
printf("%d", i);
else
printf("%d^%d", i, counter);
} else if(check!=check2){
if(counter==1)
printf(".%d", i);
else
printf(".%d^%d", check2, counter);
counter=1;
}
}
i=1;
}
prime=0;
}
counter=1;
printf("\n-------------------");
count=0;
}
printf("-------------------\nEnd");
return 0;
}
Turned out the solution was quite simple. I'm counting the power with another for loop that checks how many times I can divide by that number.
for(i=2; i<=num; i++){
if(num%i==0){
for(i; num%i==0; count++){
num/=i;
}
printf("%d %d", i, count);
i=1;
count=0;
}
}

Tell me why use "max,min,sum=arr[0];" this code

I want to print the maximum, minimum, and total sum of input integers
but i don't understand why use this code(max,min,sum=arr[0];)
#include<stdio.h>
int main(void)
{
int arr[5];
int max, min, sum, i;
for (i = 0; i < 5; i++)
{
printf("input: ");
scanf("%d", &arr[i]);
}
max = min = sum = arr[0];
for (i = 1; i < 5; i++)
{
sum += arr[i];
if (max < arr[i])
max = arr[i];
if (min > arr[i])
min = arr[i];
}
printf("Maximum: %d \n", max);
printf("Minimum: %d \n", min);
printf("Total: %d \n", sum);
return 0;
}
The code is setting all the variables equal to the first element of the array. Then compares with the rest of it to replace in case they are greater, lesser or to add the value (sum)

Read and print only positive array indexes

The question i have is similar to some questions answered, but the answer for this in particular wasn't in there. Here is my code
int main()
{ int i,j,k,sorted;
int A[4][4];
int C[16];
int positive = 0;
for(i=0;i<4;i++)
{
for(j = 0; j<4; j++)
{
printf("A[%d][%d]: ", i,j);
scanf("%d", &A[i][j]);
if(A[i][j]>0){
C[positive] = A[i][j];
printf("C = %d\n");
positive++;
}
}
}
for(j=0;j<4;j++)
{
printf("%d ", A[positive]);
}
printf("\n");
//printf("Your positive numbers are: ", positive);
printf("\n");
system("pause");
return 0;
}
I want to check if the input is negative and add it to the 1D array only then. The question is how do I do that?
General Tips
int main()
{
int i,j,k,sorted; // do not declare loop control variables outside of their respective loops
int A[4][4];
int C[16];
int positive = 0; // for readability's sake please refrain from using long ass variable names for simple control var's
for(i=0;i<4;i++) // idiomatic way is for(int i = 0; i < 4; i++)
{
for(j = 0; j<4; j++) // for(int j = 0; j<4; j++)
{
printf("A[%d][%d]: ", i,j);
scanf("%d", &A[i][j]);
if(A[i][j]>0)
{
C[positive] = A[i][j];
printf("C = %d\n"); // missing argument & redundant since you intend to print them afterwards anyway?
positive++;
}
}
}
for(j=0;j<4;j++) // this loop is supposed to print all positive numbers?
{
printf("%d ", A[positive]); // you are looping through A[0-3][0], which are not necessarily the positive numbers of A.
}
printf("\n");
//printf("Your positive numbers are: ", positive);
Now to your question at hand. What the code tries to do and what you ask from us are two different things. Right now you are adding positive values to your C array, but your question asks about negative values?
Cleaned up code that behaves the way i think you want it to.
int main()
{
int A[4][4];
int C[16];
int k = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
printf("A[%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
if (A[i][j] > 0)
{
C[k] = A[i][j];
k++;
}
}
}
printf("positive numbers in matrix A are: ");
for (int i = 0; i < k; i++) // loop from 0 to actual number of positive integers; array size remains at 16 in this case.
{
printf("%d ", C[i]);
}
printf("\n");
//printf("Your positive numbers are: ", positive);
printf("\n");
system("pause");
return 0;
}
int main()
{ int i,j,k,sorted;
int A[4][4];
int C[16];
int positive = 0;
for(i=0;i<4;i++)
{
for(j = 0; j<4; j++)
{
printf("A[%d][%d]: ", i,j);
scanf("%d", &A[i][j]);
if(A[i][j]>0){
C[positive] = A[i][j];
printf("C = %d\n");
positive++;
}
}
}
for(j=0;j<positive;j++)
{
printf("%d ", C[j]);
}
printf("\n");
//printf("Your positive numbers are: ", positive);
printf("\n");
system("pause");
return 0;
}
where you wrong is in the looping for result, you print a 2D array but you just use 1D array, and in the loop is not until 4 but until the positive, and then in the loop, you print A, but the result is C no A

Find min,max,average values throw array

I made this code but I can't stop it. This code abou finding min, max, average values. What should I do?
#include <stdio.h>
void get_stat_per_test(int score[][3])
{
int i, k, min, max, sum;
for (k = 0; k<3; k++){
min = max = score[0][k];
sum = 0;
for (i = 0; i<10; i++){
if (score[i][k]<min) min = score[i][k];
if (score[i][k]>min) max = score[i][k];
sum += score[i][k];
}
printf("Max=%d\n", max);
printf("MIn=%d\n", min);
printf("Average=%f\n", sum / (3.0 * 10));
}
}
int main(void)
{
int i, k;
int score[10][3];
for (i = 0; i<10; i++){
for (k = 0; k<3; k++){
printf("The result is %d student in %d exam: ", i, k);
scanf("%d", &score[i][k]);
}
}
get_stat_per_test(score);
return 0;
}
After student 5 should stop.

How to scan in matrix and add sum the row and col

include <stdio.h>
int main(){
int number [5][5] , row=0, col=0,sumrow[5], sumcol[5];
for(row=0;row<5;row++)
{
printf("Enter row %d ", row+1);
for(col=0;col<5;col++)
scanf("%d",&number[row][col]);
}
// I want to read in row and col of numbers
for(row=0;row<5,sumrow[row]=sumcol[col]=0;row++);
for(row=0;row<5;row++)
for(col=0;col<5;col++)
{
sumrow[row]=sumrow[row]+number[row][col];
sumcol[col]=sumrow[col]+number[row][col];
}
// here I want to plus all the row in sumrow and col in sumcol
printf("Row totals ");
for(row=0;row<5;row++) {
printf("%d ",sumrow[row]);
}
printf("\n Row totals ");
for(col=0;col<5;col++){
printf("%d ",sumrow[col]);
}
return 0;
}
This code is not working. I want to read the matrix and all rows and columns.
I don't know what the problem is.
If u want to add all elements of row and column or sum of all rows and column below is the code,
#include <stdio.h>
int main ()
{
static int array[10][10];
int i, j, m, n, sum = 0;
int rowSum = 0;
int colSum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the elements of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("Sum of the %d row is = %d\n", i, sum);
rowSum += sum;
sum = 0;
}
printf("Sum of all the row is = %d\n", rowSum);
sum = 0;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
sum = sum + array[i][j];
}
printf("Sum of the %d column is = %d\n", j, sum);
colSum += sum;
sum = 0;
}
printf("Sum of all the column is = %d\n", colSum);
}
I don't know if I understood correctly, but if you want to sum all the elements of the matrix you should use this for example:
int a[5][5],n,m,i,j, soma=0;
printf("Enter value of m and n: ");
scanf("%d%d",&m,&n);
printf("\nEnter elemets of the matrix:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&a[i][j]);
for(i=0;i<m;++i){
sum_col += i;
}
for(j=0;j<n;++j){
sum_row += ;
}
printf("\nYour sum is: %d", soma);
getch();
return 0;
PS: I also used conio library, so you should use #include<conio.h> at the beginning
But if you want to sum the numb of rows and the numb of cols and print one by one, you should use this one:
int number [5][5] , row=0, col=0,sumrow=0, sumcol=0, cont_1=0, cont_2=0;
for(row=0;row<5;row++)
{
printf("Enter row %d ", row+1);
for(col=0;col<5;col++)
scanf("%d",&number[row][col]);
}
// I want to read in row and col of numbers
for(row=0;row<5;row++){
cont_1++;
for(col=0;col<5;col++)
{
sumcol += number[row][col];
}
printf("\nThe sum of Col %d is %d", cont_1, sumcol);
sumcol = 0;
}
for(col=0;col<5;col++){
cont_2++;
for(row=0;row<5;row++)
{
sumrow += number[row][col];
}
printf("\nThe sum of Row %d is %d", cont_2, sumrow);
sumrow = 0;
}
return 0;
I just made your code more clear, so you should understand what you are doing.

Resources