Im taking a C programming beginning course, and have a problem with a for loop.
I want to make two double arrays. One that takes input from user, and one that sums the inputs.
I then want to output the two arrays. One that shows the input, and one that shows the sums og the input in every cell.
The problem comes when I try to show the sum for every "cell" in the array.
The output just becomes the same as the input.
I can solve it with:
// print all the numbers in the second array
for (i = 0; i < n; i++) {
sum = sum + a[i];
printf("%lf ", sum);
but then the assignment wouldn't be solved. Hope you can educate me.
#include <stdio.h> #include <stdlib.h>
int main(void) {
int n; //numbers of cells
int i; //The numbers in the cells
printf("How many cells in the array would you like? \n");
scanf("%d", &n);
double a[n], sum=0; // declare an array and a loop variable.
double b[n], sumo=0;
printf("Enter the numbers:\n");
for (i = 0; i < n; i++) { // read each number from the user
scanf("%lf", &a[i]);
}
printf("The results, my lord:\n");
// print all the numbers in the first array
for (i = 0; i < n; i++) {
printf("%lf ", a[i]);
}
printf("\n"); //THIS IS WHERE THE PROBLEM STARTS
// print all the numbers in the second array
for (i = 0; i < n; i++) {
b[i] = b[i] + a[i];
printf("%lf ", b[i]);
}
return 0;
}
For starters the array b is not initialized
double b[n], sumo=0;
So this statement
b[i] = b[i] + a[i];
invokes undefined behavior.
It seems what you need is the following for loop
for (i = 0; i < n; i++) {
b[i] = a[i];
if ( i != 0 ) b[i] += a[i-1];
printf("%lf ", b[i]);
}
You can calculate the sum (aka the b array) when you read the user data:
for (i = 0; i < n; i++)
{
scanf("%lf", &a[i]);
if (i == 0)
{
b[i] = a[i];
}
else
{
b[i] = b[i-1] + a[i];
}
}
or do it like:
scanf("%lf", &a[0]);
b[0] = a[0];
for (i = 1; i < n; i++)
{
scanf("%lf", &a[i]);
b[i] = b[i-1] + a[i];
}
or do it like:
sum = 0;
for (i = 0; i < n; i++)
{
scanf("%lf", &a[i]);
sum += a[i];
b[i] = sum;
}
The last part is how I solved the problem. I found the issue that. I didn't update the "sum" for ever loop. It adds a new value in b[I] this way. :-)
double a[n], sum=0; // declare an array and a loop variable.
double b[n];
printf("Enter the numbers:\n");
for (i = 0; i < n; i++) { // read each number from the user
scanf("%lf", &a[i]);
}
printf("The results, my lord:\n");
// print all the numbers in the first array
for (i = 0; i < n; i++) {
printf("%.2lf ", a[i]);
}
printf("\n");
// print all the numbers in the second array
for (i = 0; i < n; i++) {
sum = sum + a[i];
b[i] = sum;
printf("%.2lf ", b[i]);
Related
I have this piece of code that is supposed to sum all the even numbers but it isnt working
int n, k = 0, sum = 0;
for (i = 0; i < n; i++)
if (s[i] % 2 == 0) {
k++;
for (int i = 0; i < n; i++)
sum += s[i];
printf("%i ", s[i]);
}
printf("\n sum of even numbers=%i", sum);
int i, sum = 0;
for (i=0; i < n; i++) //n is the number of elements in the array
{
if (s[i] % 2 == 0)
sum = sum + s[i];
}
printf("Sum of the even numbers = %d",sum);
There was no need for a nested loop. Just delete the inner for
everyone!
I create matrix with random elements:
static int array[10][10];
int i, j, m, n, sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf("%d ", rand() % 10);
}
printf("\n");
}
And this is one function to sum row elements:
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);
sum = 0;
}
My problem is that program wont to sum it properly, program just print 0 for result.
Also, when I add manual elements, program works fine, but wont with random numbers.
Someone to help me? Thank you!
You haven't actually assigned the values to the array; you're just printing them.
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
int num = rand() % 10;
printf("%d ", num);
array[i][j] = num;
}
printf("\n");
}
is probably what you want.
Also the array has space for only 10 x 10 elements. So you need to validate that the inputs for m and n do not exceed that limit.
The default value of int type in C is 0. Because you do not enter the value to array, so all of values of this array are equal to 0. So sum = 0 after all calculations.
Try to initialize values for this array. For example, in the first for loop (the for loop content printf with rand() function):
array[i][j] = rand() % 10;
OR
array[i][j] = 1; // or whatever you want
I wrote to program in C to attempt to print array elements in descending order. I wrote a nested loop which would find the maximum element of the array and the value of the element would be set to later 0. This process would be repeated for all the elements. However, in the output, I am getting the first 2-3 values as desired but the remaining values are garbage. Any suggestions?
int main() {
int i, j, n, k;
scanf("%d\n", &n);
int a[100], b[100];
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++) {
int max = a[i];
for (j = i; j < n; j++) {
if (a[j] > max) {
max = a[j];
b[i] = max;
}
}
for (k = 0; k < n; k++) {
printf("%d", a[k]);
if (a[k] == b[i]) {
a[k] = 0;
}
}
printf("\n");
}
for (i = 0; i < n; i++) {
printf("%d ", b[i]);
}
}
The main issue is that you only set b[i] = max; when you find a new max, but since you initialized max to be a[i] it could happen that it already holds the maximum value. So the if never executes, therefore b[i] is not written and there's garbage value in it. You should move this line from the if after that for loop.
Another issue is that you initialize j with i in this loop. You should initialize it to 0.
The changed part:
for (j = 0; j < n; j++) {
if (a[j] > max) {
max = a[j];
}
}
b[i] = max;
I am trying to copy elements from two arrays into a third.
I can't understand why it doesn't work.
I made sure that the two arrays are filled properly, but for some reason, the actual copying doesn't work- when I print the elements of the arr3, I get some random numbers.
#include <stdio.h>
int main()
{
int arr1[10], arr2[10], arr3[20];
int i, n;
printf("Enter a number of elements to be stored in each array (up to 10): ");
scanf("%d", &n);
printf("Enter the %d elements to the first array:\n", n);
for (i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr1[i]);
}
printf("Enter the %d elements to the second array:\n", n);
for (i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr2[i]);
}
/*
// A test to make sure first 2 array are filled by the user- works
for(i = 0; i < n; i++)
printf("%d ", arr1[i]);
for(i = 0; i < n; i++)
printf("%d ", arr2[i]);
*/
// something wrong here, the elements are not coppied to the third array
for(i = 0; i < n; i++);
arr3[i] = arr1[i];
for(i = n; i < 2 * n; i++)
arr3[i] = arr2[i];
for(i = 0; i < 2 * n; i++)
printf("%d\n", arr3[i]);
return(0);
}
You're reading past the end of arr2, try this;
for (i = 0; i < n; i++)
arr3[i] = arr1[i];
for (i = 0; i < n; i++)
arr3[n+i] = arr2[i];
That's because your code is reading arr2[10],arr2[11] ..... arr2[19] (if n=10 ), all these values do not exist because arr2 only has 10 values. you can use this.
for (i=0; i<n; i++)
arr3[n+i]=arr2[i];
or
for (i=n; i<n*2; i++)
arr3[i]=arr2[i-n];
I have the following code which is driving me nuts. I am trying to input values into a 2-dimensional array.
I do not really know how I can insert a value in to specific location in the array. For example I want to enter the values in a pyramid fashion. I should be able to add one more value in each level.
a[0][0] ===> 1
a[1][0] & [1][1] &[1][2] ===> 2 3
a[2][0] & [2][0] & [2][2] ===> 4 5 6 etc.
I also want to be able to store the greatest integer In each level so that I can sum all the large integers in each level.
So far with the following code, I am unable to figure out how to insert the value and I also cannot sum the greatest values in each level.
for(i = 0; i < 2; i++){
for(j = 0; j <= 4; j++){
printf("Enter the values in to the array");
scanf("%d",&arr[i][j]);
}
}
for(i = 0; i < 2; i++){
for(j = 0; j <= 4; j++){
if(arr[i][j] > arr[i][j+1]){
holder = arr[i][j];
}else{
holder = arr[i][j+1];
}
}
sum = sum+holder;
}
printf("%d\n\n",sum);
To enter the values in a pyramid fashion & calculate the sum of the largest numbers of each row, try the code below:
int arr[5][10];
int i,j;
int holder=0;
int sum = 0;
for (i=0;i<5;i++)
{
for(j=0; j<=i;j++)
{
printf("Enter your input: \n");
scanf("%d", &arr[i][j]);
if(arr[i][j] > holder)
{
holder = arr[i][j];
}
}
printf("Largest Value in Row %d is %d\n", i, holder);
sum = sum + holder;
holder = 0;
}
printf(" Sum = %d\n", sum);
Try the code below for dynamically deciding the array size and assigning the values:-
int **arr;
int *greatest_each_lavel;
int n, i, j, temp;
printf ("enter the level.\n");
scanf ("%d", &n);
if (n<1)
{
printf ("Wrong input.\n");
exit (1);
}
greatest_each_level = (int *) malloc (n * sizeof (int));
arr = (int **) malloc (n * sizeof (int *));
for (i=1; i<=n; i++)
{
arr[i] = (int *) malloc (i * sizeof (int));
}
// Populate the array members.
for (i=0; i<n; i++)
{
temp = 0;
for (j=0; j<=i; j++)
{
printf ("Enter the number ");
scanf ("%d", &arr[i][j]);
if (arr[i][j] > k)
{
temp = arr[i][j];
}
}
greatest_each_level[i] = temp;
}
// To get the sum of greatest number at each level
int sum = 0;
for (i = 0; i < n; i++)
sum = sum + greatest_each_level[i];
printf ("Sum is : %d\n", sum);
// When all task are done, dont forget to free the memory.
for (i=0; i<n; i++)
{
free (arr[i]);
}
free (arr);
free (greatest_each_level);
greatest_each_level = NULL;
arr = NULL;