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;
}
Related
I'm writing a program which scanf integers and printf in double.
Here is my code:
int main(void) {
int arraySize;
scanf("%d",&arraySize);
double vector[arraySize];
for(int i=0;i<arraySize;i++) scanf("%lf", &vector[i]);
for(int a=0;a<arraySize;a++) printf("VECTORS:[%lf]",vector[a]);
}
Since I need to for loop every element in the array then printf all of them one by one.
this is the output I had:
VECTORS:[1.000000] VECTORS:[2.000000] VECTORS:[3.000000]
How can I change the format of the printf function and get ouput like this:
VECTOR: [ 1.000, 2.000, 3.000 ]
Your one major mistake is your array size. I know your compiler won't issue any warning but this is not any feature which language provide so size must be a
constant numerical value or const expression.
So in short you can't create array After asking size from user. This is completely wrong.
int arraySize;
scanf("%d",&arraySize);
double vector[arraySize];
You must make size const. If you want less values than the declared size you can decrease the no of times for loop will run but you can't decide array size as inputted by user.
const int size = 10; // this is how your size should be. Even your compiler allowed VLA you should not try this. size of arrays must be constant.
int main()
{
unsigned int i,s;
int arr[size];
printf ("Enter the size of array.");
scanf("%d",&s);
for(i = 0 ; i<s;i++){
scanf("%d",&arr[i]);
}
for(i = 0 ; i<s;i++){
arr[i] = arr[i]*arr[i];
}
for(i = 0 ; i<s;i++){
printf("%d",arr[i]);
}
}
Print VECTOR once then loop over all the vectors and output them in the desired format.
const int size = 10;
int main(void) {
double vector[size];
for(int i=0;i<size;i++)
scanf("%lf", &vector[i]);
printf("VECTOR: [ ");
for(int a=0;a<size;a++){
printf("%lf", vector[a]);
if(i < size - 1)
printf(", ");
}
printf(" ]");
}
You need to allocate memory dynamically to use it.
Use the printf in the code below to define precision.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int arraySize = 0;
scanf("%d",&arraySize);
double *vector = (double *) calloc(arraySize, sizeof(double));
for(int i=0; i<arraySize; i++) {
scanf("%lf", &vector[i]);
}
printf("VECTORS:[");
int a;
for(a=0;a<arraySize;a++) {
printf(" %.3lf",vector[a]);
if (a<(arraySize-1)) {
printf(",");
} else {
printf(" ");
}
}
printf("]");
}
Change your for loop to take 3 elements at once.
for(a=0;a<arraySize;a+=3) {
printf("VECTORS:[%lf", vector[a]);
if (a+1 < arraySize) printf(", %lf", vector[a+1]);
if (a+2 < arraySize) printf(", %lf", vector[a+2]);
printf("]\n");
}
#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;
}
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.
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.
I would like to get your help to understand and finish my program.
This is what I have to do:
"You must exercise program that:
First. An absorbing two - dimensional integer arr [M] [N]. M - number of rows N - number of columns. (Matrix size was received from the user)
Two. The program uses auxiliary functions "shift" moves the values of the matrix to the right one place, as shown in the picture (2 entered instead of 1, 3 instead of 2, 4 instead of 3, ... 20 instead of 19, first place 20).
Shift have to write a function and call her three times in the sample matrix loop .."
Example
My problems are:
I don't know how to do the matrix two - dimensional integer arrays that there size is entered by the user. I only know by DEFINE SIZE of the row and the cols
My function isn't close to the real deal so I would like to get help finish my function.
My output:
My code:
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#define M 4
#define N 5
void shift (int arr[M][N], int length);
void main()
{
int arr[M][N];
int i,j,length;
printf("Enter %d rows \n",M);
for (i=0 ; i<M ; i++ )
{
printf("Enter %d numbers:\n",N);
for(j=0 ; j<N ; j++ )
{
scanf("%d" , &arr[i][j] );
}
length=N+M;
}
shift (arr,length);
system("pause");
return ;
}
void shift (int arr[M][N], int length)
{
int i,j,temp;
temp=arr[0][0];
for(i=0; i<M; i++)
{
for(j=0; j<N-1 ; j++)
{
printf("%d ",arr[i][j]);
}
arr[i][j]=temp;
printf("\n");
}
}
Edit: pictures resized
Shifts all columns to the right.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void shift_columns_right(int M[100][100], int rows, int cols) {
int tmp_lastcol;
int j, k;
for (j = 0; j<rows; j++){
tmp_lastcol = M[j][cols-1];
for (k = cols-1; k > 0; k-- ){
M[j][k] = M[j][k-1];
}
M[j][0] = tmp_lastcol;
}
}
int main(void){
int B[100] [100] = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16},
{17,18,19,20},
};
shift_columns_right(B,5,4);
return 0;
}
I will you give a hint as how to shift the elements. The logic is to swap the elements between the current and last element in the row as you iterate. I will show you a working example on 1D array.
#include <stdio.h>
#define ARRAY_SIZE 5
int main()
{
int a[ARRAY_SIZE] = {11,22,33,44,55};
int i;
for (i=0; i<ARRAY_SIZE; ++i)
{
int temp = a[i];
a[i] = a[ARRAY_SIZE-1];
a[ARRAY_SIZE-1] = temp;
}
for(i=0; i<ARRAY_SIZE; ++i)
{
printf("%d\t",a[i]);
}
return 0;
}
Output: 55 11 22 33 44
To dynamically allocate the memory for array, use malloc. Hope it helps !