How to sum two arrays in another array in C? - c

I want to try to get two arrays and sum them to another array, but it just doens't work, and I don't know why.
My code :
int v1[3],v2[3],v3[3];
for(int i = 0 ; i < 3; i++) {
printf("Type a number : \n");
scanf("%d", &v1[i]);
}
for(int i = 0 ; i < 3; i++) {
printf("Type a number : \n");
scanf("%d", &v2[i]);
}
for(int i = 0 ; i < 3; i++) {
v3[i] = v1[i] + v2[i];
scanf("%d", &v3[i]);
printf("Total : %d ", &v3[i]);
}
And when I get to type all the 6 numbers, he just don't show Total, and I have to type more to go to exit screen.
Screenshot :

What do you exactly want? Summation of each element from two arrays into a new third array? That's it right?
int main(int argc, char** argv) {
int v1[3],v2[3],v3[3];
for(int i = 0 ; i < 3; i++) {
printf("Type a number for v1 :\t");
scanf("%d", &v1[i]);
printf("Type a number for v2 :\t");
scanf("%d", &v2[i]);
// Add here
v3[i] = v1[i] + v2[i]; // Mind you that this could lead to integer overflow.
}
printf("\nResult Arr :\n");
for(int i = 0 ; i < 3; i++)
printf("%d\n", v3[i]);
}

What is the purpose of scanf inside the third for loop?
I think remove scanf inside third for loop:
scanf("%d", &v3[i]);
Also, remove & in printf:
printf("Total : %d ", v3[i]);
Full code: This code working fine on GCC compiler.
#include <stdio.h>
int main()
{
int v1[3],v2[3],v3[3], i;
for(i = 0 ; i < 3; i++)
{
printf("Type a number : \n");
scanf("%d", &v1[i]);
}
for(i = 0 ; i < 3; i++)
{
printf("Type a number : \n");
scanf("%d", &v2[i]);
}
for(i = 0 ; i < 3; i++)
{
v3[i] = v1[i] + v2[i];
printf("Total : %d\n", v3[i]);
}
}

Delete the third scanf and you don't want the ampersand before the v3 in the printf.

First, initialize your array so you won't have a garbage.
`int v1[3] = {0,0,0}, v2[3] = {0,0,0}, v3[3] = {0,0,0};`
in adding both arrays,
for(int i = 0; i < 3; i++){
v3[i] = v1[i] + v2[i];
printf("total: %d", v3[i]);
}

If you want to store result in v3, you need to remove scanf("%d", &v3[i]); from last loop
You need to change
printf("Total : %d ", &v3[i]);//you are passing address here, where printf expects value
to
printf("Total : %d ", v3[i]);
Also you should add a space before %d in each scanf so that it takes care of enter hit from previous input
eg
scanf(" %d", &v1[i]);

It is because you added a scanf function in the last for loop which does the summation.
So if you just remove that scanf line it will work great.
Old code:
int v1[3],v2[3],v3[3]; for(int i = 0 ; i < 3; i++) { printf("Type a number : \n"); scanf("%d", &v1[i]); } for(int i = 0 ; i < 3; i++) { printf("Type a number : \n"); scanf("%d", &v2[i]); } for(int i = 0 ; i < 3; i++) { v3[i] = v1[i] + v2[i]; scanf("%d", &v3[i]); printf("Total : %d ", &v3[i]); }
New code should be
int v1[3],v2[3],v3[3]; for(int i = 0 ; i < 3; i++) { printf("Type a number : \n"); scanf("%d", &v1[i]); } for(int i = 0 ; i < 3; i++) { printf("Type a number : \n"); scanf("%d", &v2[i]); } for(int i = 0 ; i < 3; i++) { v3[i] = v1[i] + v2[i]; printf("Total : %d ", &v3[i]); }
That should do it.

Related

how can I make a loop print out missing elements in my int array?

I've been trying to solve an assignment where you:
Enter the size N of an array of ints
Enter the max value M of the ints that you want to populate the array with
Enter N values that are of a value between 1 and M, into a second array.
compare these two and print out the missing numbers...
Like this:
Size of array? 10 // => N = 10
Max value in array? 8 // => M = 8
Please enter 10 values between 1 and 8:
4 1 3 1 7 3 4 4 6 1
Missing values: 2 5 8
for some reason, my for loop just prints out all the numbers between 1 and M instead, no matter what I try... What am I missing??
code:
#include <stdio.h>
int main(void)
{
int aSize, mValue;
printf("Size of array? ");
scanf(" %d", &aSize);
printf("Max value in array: ");
scanf(" %d", &mValue);
int table[aSize];
int values[mValue];
for (int i = 0; i < aSize; i++)
{
table[i] = i+1;
if ((i+1) > mValue)
{
table[i] = 0;
}
}
printf("Please enter %d values between 1 and %d:\n", aSize, mValue);
for (int i = 0; i < mValue; i++)
{
scanf(" %d", &values[i]);
}
for(int i = 0; i < aSize; i++)
{
for (int j = 0; j < mValue; j++)
{
if(table[i] != values[j] && table[i] != 0)
{
printf("%d ", table[i]);
break;
}
}
}
}
#include <stdio.h>
int main()
{
int aSize, mValue;
printf("Size of array? ");
scanf(" %d", &aSize);
printf("Max value in array: ");
scanf(" %d", &mValue);
int table[aSize];
int values[aSize]; // not 'mSize' because it is just max value not size of array
for (int i = 0; i < aSize; i++)
{
table[i] = i+1;
if ((i+1) > mValue)
{
table[i] = 0;
}
}
printf("Please enter %d values between 1 and %d:\n", aSize, mValue);
for (int i = 0; i < aSize; i++)
{
scanf(" %d", &values[i]);
}
for(int i = 0; i < aSize; i++)
{
int flag=0;
for (int j = 0; j < aSize; j++)
{
if(table[i] == 0 || table[i] == values[j]) // numbers in common or zero
{
flag=1;
break;
}
}
if(flag == 0) printf("%d",table[i]); // missing numbers
}
}
In this code, I'm creating an extra array to store the occurance of the variable, then printing it. 1 means present 0 means not present.
#include <stdio.h>
int main(void) {
int aSize, mValue;
printf("Size of array? ");
scanf(" %d", &aSize);
printf("Max value in array: ");
scanf(" %d", &mValue);
int table[aSize];
int values[mValue+1];
for (int i = 0; i <= mValue; i++)
{
values[i]=0;
}
values[0]=1;
printf("Please enter %d values between 1 and %d:\n", aSize, mValue);
for (int i = 0; i < aSize; i++)
{
scanf(" %d", &table[i]);
if(table[i]>mValue || table[i]<1){
printf("Enter numbers in given range");
return 0;
}
}
for(int i = 0; i < aSize; i++)
{
values[table[i]]=1;
}
printf("Missing values:\n");
for(int i = 1; i <= mValue; i++)
{
if(!values[i])
printf("%d\n",i);
}
return 0;
}
This is better version of the above logic.
#include <stdio.h>
int main(void) {
int aSize, mValue;
printf("Size of array? ");
scanf(" %d", &aSize);
printf("Max value in array: ");
scanf(" %d", &mValue);
int temp;
int values[mValue+1];
values[0]=1;
printf("Please enter %d values between 1 and %d:\n", aSize, mValue);
for (int i = 0; i < aSize; i++)
{
scanf(" %d", &temp);
if(temp>mValue || temp<1){
printf("Enter numbers in given range");
return 0;
}
values[temp]=1;
}
printf("Missing values:\n");
for(int i = 1; i <= mValue; i++)
{
if(!values[i])
printf("%d\n",i);
}
return 0;
}

How to remove the last comma on a number , and bug with decimals?

I wrote the code, I'm learning array, how can it not print my last comma on the numbers, and how does it load this decimal for me properly?
input n: 7
numbers: 7 1 0 2.1 -2 5 7 3
output: {-2,0,1,2,3,5,7,}
output which I need: {-2,0,1,2.1,3,5,7}
#include <stdio.h>
int main() {
int i, j, a;
float n;
float num[100];
printf("Input number n: \n");
scanf("%f", &n);
while (n < 1) {
printf("Wrong input!\n");
printf("Input numbers array: \n");
scanf("%f", &n);
}
printf("Input %g number: \n", n);
for (i = 0; i < n; ++i)
scanf("%f", &num[i]);
for (i = 0; i < n; ++i) {
for (j = i + 1; j < n; ++j) {
if (num[i] > num[j]) {
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
printf("\n{");
for (i = 0; i < n; ++i) {
printf("%g,", num[i]);
}
printf("}");
return 0;
}
In addition to the i486 answer and to solve the decimal part problem, you must be aware that by declaring
int a
whenever you do
a = num[i]
you are 'converting' num[i] to integer and therefore you lose the decimal part.
that's why it must be
float a
Method 1:
for (i = 0; i < n; ++i)
printf( "%g%s", num[i], i == n - 1 ? "" : "," );
Method 2:
for (i = 0; i < n - 1; ++i) {
printf( "%g,", num[i] );
}
printf("%g}", num[n - 1]);
Method 3: (after nielsen comment)
for (i = 0; i < n; ++i)
printf( i ? ",%g" : "%g", num[i] );

Tried to create sorting program, but isn't working as expected

The following code is written for sorting array.
Here the program gives unexpected output results.
The error should be in line 10-19.
#include<stdio.h> //Sorting array program
int main()
{
int arr[20],i,j,n,temp;
printf("Enter number of elements : ");
scanf("%d",&n);
printf("\nEnter the elements of the array : ");
for (i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
{
for(j=0;j<arr[i];j++)
if (arr[i+1]<arr[i])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
printf("\nThe sorted list is : \n");
for (i=0;i<n;i++)
printf("\n arr[%d] : %d",i,arr[i]);
return 0;
}
Output ::
Enter number of elements : 4
Enter the elements of the array : 5
3
2
1
The sorted list is :
arr[0] : 3
arr[1] : 2
arr[2] : 1
arr[3] : -16777216
Process returned 0 (0x0) execution time : 8.161 s
Press ENTER to continue.
Modify your program likes this and you'll find out what happens:
#include <stdio.h> //Sorting array program
#include <assert.h>
int main()
{
int arr[20], i, j, n, temp;
printf("Enter number of elements : ");
scanf("%d", &n);
printf("\nEnter the elements of the array : ");
for (i = 0; i<n; i++)
scanf("%d", &arr[i]);
for (i = 0; i<n; i++)
{ // <<< line added
for (j = 0; j < arr[i]; j++)
{
assert(j < n); // <<< line added
assert(i + 1 < n); // <<< line added
if (arr[i + 1] < arr[i])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
} // <<< line added
}
printf("\nThe sorted list is : \n");
for (i = 0; i<n; i++)
printf("\n arr[%d] : %d", i, arr[i]);
return 0;
}
Read about assert here.
But anyway the algorithm looks fishy to me anyway:
This line is particularly suspicious:
for (j = 0; j < arr[i]; j++)
Try this
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h> //Sorting array program
int main()
{
int arr[20], i, j, n, temp;
printf("Enter number of elements : ");
scanf("%d", &n);
printf("\nEnter the elements of the array : ");
for (i = 0; i<n; i++)
scanf("%d", &arr[i]);
for (i = 0; i<n; i++)
{
for (j = 0; j<n; j++)
if (arr[i]<arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
printf("\nThe sorted list is : \n");
for (i = 0; i<n; i++)
printf("\n arr[%d] : %d", i, arr[i]);
printf("\n");
return 0;
}

How to delete duplicated values in array in C?

I want to delete duplicates values in array. For example: array[1,5,6,1,3,5,9] I want to have array[6,3,9].
I have written this, but I am having troubles:
#include<stdio.h>
main() {
int array[50], i, j, k=0, c=0, array2[50], n;
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("array[%d]= ", i); scanf("%d", &array[i]);
}
for (i = 0; i < n; ) {
for (j = i + 1; j < n; j++) {
if (array[i] == array[j])
i++;
else {
array2[k++] = array[i];
c++;
}
}
}
for (k = 0; k < c; k++) {
printf("%d ", array2[k]);
}
system("pause");
}
You should start by describing your problem in pseudo code, and breaking it into smaller pieces.
Start from scratch by deleting all these redundant variables, and try to implement the following algorithm:
for each element in inputArray
if not elementIsDuplicate(element)
add to outputArray
That's a single for loop. The elementIsDuplicate is separate function.
Now try to implement the function elementIsDuplicate. This function also contains a single loop, takes input parameters (int* array, int n, int currentIdx) and returns 1 or 0 indicating whether the element at currentIdx occurs anywhere else in the array.
#include<stdio.h>
int main(){
int array[50], i, j, k=0, c, n, array2[50] = {0};
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; ++i){
int num, dup = 0;
printf("array[%d]= ", i); scanf("%d", &num);
for(j = 0; j < k; ++j){
if(array[j] == num){
array2[j] = dup = 1;
break;
}
}
if(!dup){
array[k++] = num;
}
}
for (c=i=0; i < k; ++i){
if(!array2[i])
printf("%d ", array[c++] = array[i]);
}
printf("\n");
/*
for(i=0;i<c;++i)
printf("%d ", array[i]);
printf("\n");
*/
system("pause");
return 0;
}
#include<stdio.h>
main()
{
int n, a[50], b[50], count = 0, c, d;
printf("Enter number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c=0;c<n;c++)
scanf("%d",&a[c]); //enter array elements
for(c=0;c<n;c++)
{
for(d=0;d<count;d++)
{
if(a[c]==b[d])
break;
}
if(d==count)
{
b[count] = a[c];
count++;
}
}
printf("count is: %d\n",count);
printf("Array obtained after removing duplicate elements\n");
for(c=0;c<count;c++)
printf("%d\n",b[c]);
return 0;
}
This will remove multiple duplicates from the desired array..
Example: if the input array is: 3 6 5 6 2 8 6 5 9 8 6 ,,then the output array will be: 3 6 5 2 8 9

'for' loop fail in multidimentional array

Question is: Checking two matrices, if one is a sub matrix of another.
The problem I am facing here is a for loop as commented as "//problem" in the code. When for the first time the program runs, the mentioned for loop does not work as it should.
#include <stdio.h>
#define N 10
int
main ()
{
char matrix1[N][N], matrix2[N][N];
int i, j, row1, col1, row2, col2, k, l, m, n, check = 0;
//First Matrix
printf ("Enter the data\n");
printf ("Enter the size of rows\n");
scanf ("%d", &row1);
printf ("Enter the size of columns\n");
scanf ("%d", &col1);
printf ("Now enter the values please\n");
//Putting Values In First Matrix
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
printf ("Please enter the %dth row and %dth column\n", i + 1,
j + 1);
scanf ("%s", &matrix1[i][j]);
}
}
//Second Matrix
printf ("Enter the data\n");
printf ("Enter the size of rows\n");
scanf ("%d", &row2);
printf ("Enter the size of columns\n");
scanf ("%d", &col2);
printf ("Now enter the values please\n");
//Putting Values In Second Matrix
for (i = 0; i < row2; i++)
{
for (j = 0; j < col2; j++)
{
printf ("Please enter the %dth row and %dth column\n", i + 1,
j + 1);
scanf ("%s", &matrix2[i][j]);
}
}
//Checking Both Matrices
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
if (matrix1[i][j] == matrix2[0][0])
{
k = i;
l = j;
for (m = 0; m < row2; m++)
{
for (n = 0; n < col2; n++)
{ //problem
if (matrix1[k][l] == matrix2[m][n])
{
check++;
printf ("Checked\n");
}
l++;
}
l = j;
k++;
}
}
}
printf ("hello\n");
}
if (check == row2 * col2)
{
printf ("It exists\n");
}
else
{
printf ("It doesn't exist\n");
}
}
Here is the output:
Checked
hello
Checked
Checked
Checked
Checked
hello
hello
It doesn't exist
You need to reset check to zero before starting to find sub-matrix.
Also to break once you found it (or have flag to indicate if its found).
As from your output, (assuming you are trying to find 2x2 matrix) it found it Checked printed times continuously, but its value would be 5 counting for 1st print as well, which makes your program to print "It does not exist".
Like:
int is_found = 0;
... //some code
//Checking Both Matrices
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
check = 0; //reset check
if (matrix1[i][j] == matrix2[0][0])
{
... //your code to check matrix.
...
}//if end
if(check == row2*col2)
{
is_found = 1;
}
...
} //for j end
if(is_found)
break;
...
...
if(is_found)
printf("It exists\n");

Resources