It just creates random values
I tried using a separate value for the variable of the array and I also don't know why it starts counting at element 6.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int array[3][5];
for (i = 0; i<5; i++);
{
printf("Input a whole number for row 1 element %d:\n", i+1);
scanf("%d", &array[0][i]);
}
printf("Row 1 elements:\n");
for(i = 0; i<5; i++)
{
printf("%d\n", array[0][i]);
}
return 0;
}
Ouput:
> Input a whole number for row 1 element 6: 4 Row 1 elements: 0 0 0 0
> 1897665488
>
> Process returned 0 (0x0) execution time : 1.969 s Press any key to
> continue.
It starts counting from 6 because the line for (i = 0; i < 5; i++);, is iterating (incrementing) i 5 times so, i becomes 5, then you print i + 1 to stdout.
So, basically your call to printf() and scanf() functions were never a part of any sort of loop.
NOTE: Adding a semi-colon ;, after any loop means that there is no body for the loop. Basically it's an empty loop. It can be useful for finding the length of a string, and so on.
Some tips:
Also instead to using bare return 0;, use return EXIT_SUCCESS;, which is defined in the header file stdlib.h.
use int main(void) { }, instead of int main() { }
always check whether scanf() input was successful or not
Correct Code
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
int array[3][5];
for (i = 0; i < 5; i++)
{
printf("Input a whole number for row 1 element %d:\n", i + 1);
if (scanf("%d", &array[0][i]) != 1)
{
perror("bad input: only numbers are acceptable\n");
return EXIT_FAILURE;
}
}
printf("Row 1 elements:\n");
for (i = 0; i < 5; i++)
{
printf("%d\n", array[0][i]);
}
return EXIT_SUCCESS;
}
Output:
Input a whole number for row 1 element 1:
1
Input a whole number for row 1 element 2:
2
Input a whole number for row 1 element 3:
3
Input a whole number for row 1 element 4:
5
Input a whole number for row 1 element 5:
7
Row 1 elements:
1
2
3
5
7
Related
I am trying to solve a problem where i need to input n numbers in a array and need to find is the value of the array is whether odd or even (means 2 2 2 2 will accepted but 1 2 1 3 will not gonna accepted cz it has both even and odd number) but when i am trying to print the value it is showing yesyesyes how can i do it??
#include <stdio.h>
#include <stdlib.h>
int main()
{
int t,n,a[1000],i;
scanf("%d",&t);
if(t>=1 && t<=100)
{
scanf("%d",&n);
if(n>=2 && n<=50)
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]%2==0)
{
break;
}
if(a[i]%2!=0)
{
break;
}
else
printf("NO");
}
printf("YES");
}
}
return 0;
}
I presume the problem statement:
Task: To find if all the elements in a given list are all-odd or all-even.
There are T test cases, 1 <= T <= 100
In each test case N specifying the number of elements in a list(array), 2 <= N <= 50
Followed by N space separated integers.
For each test case print YES if all list-members are all-odd or all-even. Print NO otherwise.
Since the inputs are curated, we're assuming scanf() always succeeds.
We don't need to store numbers in an array, as we don't need them later.
Simplified code :
#include <stdio.h>
#include <stdlib.h>
int main () {
int T; // number of tests
scanf ("%d", &T);
while (T--) {
int N; // numbers in a given test
scanf ("%d", &N);
int even = 0;
int odd = 0;
for (int ni = 0; ni++ < N; ) {
int X;
scanf ("%d", &X);
(X % 2) ? ++odd : ++even;
//if (odd && even) break; // but, you need to clear the inputs before next test case
}
if (N == odd || N == even)
printf ("YES\n");
else
printf ("NO\n");
}
return 0;
}
For input file:
3
4
2 2 2 2
4
1 2 1 3
5
1 5 7 33 5
Output will be:
YES
NO
YES
sorry, im still try to figure out about multiple repetition and array and i need help to find sum of each value of column in multidimensional array.
so i have this input :
2 -> number of tc
3 -> size of array
1 2 3
4 5 6 -> all value of array
7 8 9
4 -> size of array
1 2 3 4
5 6 7 8 -> value of array
9 0 1 2
3 4 5 6
and the output will be :
Case#1: 12 15 18
Case#2: 18 12 16 20
and here is my code :
int n,m;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&m);
int o[m][m],sum[m][m];
for(int i=1;i<=m;i++){
for(int j=1;j<=m;j++){
scanf("%d",&o[i][j]);
sum[i][j]=o[i][j]+o[i+1][j+1];
}
}
for(int i=1;i<=m;i++){
for(int j=1;j<=m;j++){
printf("Case #%d: %d ",i,sum[i][j]);
}
printf("\n");
}
}
return 0;
}
First the code :
#include <stdio.h>
int main () {
int cases;
printf ("\nCases Count? : ");
scanf ("%d", &cases);
for (int cid = 0; cid < cases; ++cid) {
int size;
printf ("\nMatrix Size : ");
scanf ("%d", &size);
long sum [size]; // if long has more space than int on your machine, else use long long
for (int col = 0; col < size; ++col) // reset sums before next case
sum[col] = 0;
// we don't need to store matrix, as we're not re-using it
for (int row = 0; row < size; ++row) {
for (int col = 0; col < size; ++col) {
int value;
scanf ("%d", &value);
sum[col] += value; // add the value to respective column total
}
}
printf ("Case #%d: ", cid + 1);
for (int col = 0; col < size; ++col)
printf ("%ld ", sum[col]);
printf ("\n");
}
return 0;
}
Warning: scanf() fails if you input a string instead of numbers. scanf() usage. Lookup how to use fgets() & parse inputs to have more control.
There is no prize for using bare minimum variable names. Use concise yet meaningful variable names, even if you're testing something.
Enable all compiler warnings. For GCC an alias something like alias mygcc='gcc -Wall -Wextra -pedantic -g3 -O2 -fsanitize=address,undefined,leak -Wshadow'.
I have a few suggestions to help you along. First, the first, second, and fourth for loops use the same indexing variable i. This will cause issues with the first for loop as the variable i is being changed by the others. I suggest you first create a program that only works with a single matrix then, once it works correctly, expand it to handle multiple matrices. This will reduce the number of for loops you have to manage while figuring out the indexing. Second, the sum array doesn't have the correct size. It looks like it should be sum[n][m] instead of sum[m][m]. Lastly, to get the output you want, you should move the output for loop outside of the first for loop. Like this:
int n,m;
scanf("%d",&n);
for(int i=1;i<=n;i++){
...
for(int i=1;i<=m;i++){
...
}
}
for(int i=1;i<=m;i++){
for(int j=1;j<=m;j++){
printf("Case #%d: %d ",i,sum[i][j]);
}
printf("\n");
}
I currently am having a small trouble in my code, I am supposed to make a program that adds / sums all numbers inside of an array, while I have no problem in doing that, I currently have a problem with the part in which you are supposed to scan the numbers to be put in the array, here are the example of the input
3
5
1 2 3 4 5
8
1 2 3 4 5 6 7 8
9
1 2 3 4 5 6 7 8 9
What this means is that, the user inputs number "3" as it means to create 3 arrays, the number "5" afterward means to put 5 numbers inside of the array (1 2 3 4 5), after the user has inputted the numbers inside of an array, the user inputs "8" which means to make another array consisting of 8 numbers, and then putting numbers into the array again, and so on.
However I am having a problem in which after inputting all the numbers in the array that consists of 5 number, the program instead inputs 5 number into another array again (instead of asking the amount of numbers to be put inside of another array), so instead the number "8 1 2 3 4" gets inputted in another array, and I did not know which part I did wrong.
Here are my C code :
#include <stdio.h>
int main(){
int x, y;
int i;
int n;
int c=1;
int count=0;
int sum=0;
scanf("%d", &y); //this determines the amount of array to be inputted
scanf("%d", &x); //this determines the amount of numbers to be inputted inside of an array
int line[x];
for(int i=0; i<y; i++){
sum=0;
for(int i=0; i<x; i++){
scanf("%d", &line[i]); //scan number for the array
sum += line[i];
}
printf("Case #%d: %d\n", c, sum);//output of all sum
c++;
}
}
You need to read the size for each array - currently you only read it once.
e.g.:
int numLines;
scanf("%d", &numLines);
for(int lineIdx = 0; lineIdx < numLines; lineIdx++) {
// read the number of elements for each array
int numNumbers;
scanf("%d", &numNumbers);
int line[numNumbers];
for(int i = 0; i < numNumbers; i++) {
scanf("%d", &line[i]);
}
}
Additionally you can avoid storing the individual numbers, since you're only interested in the sum, e.g.:
int sum = 0;
for(int i = 0; i < numNumbers; i++) {
int number;
scanf("%d", &number);
sum += number;
}
Also you could defer outputting the sums until all inputs have been processed, so it doesn't visually get interleaved into the input.
This would be a possible way to write that program: godbolt
#include <stdio.h>
int main() {
// get the number of arrays we need to read
int numLines;
scanf("%d", &numLines);
// The sums of each array
int sums[numLines];
for(int lineIdx = 0; lineIdx < numLines; lineIdx++) {
// read the number of elements for each array
int numNumbers;
scanf("%d", &numNumbers);
// sum up all the numbers of the array
sums[lineIdx] = 0;
for(int i = 0; i < numNumbers; i++) {
int number;
scanf("%d", &number);
sums[lineIdx] += number;
}
}
// after all arrays have been entered,
// output the sums for each case:
for(int lineIdx = 0; lineIdx < numLines; lineIdx++) {
printf("Case #%d: %d\n", lineIdx, sums[lineIdx]);
}
}
This is a code for input-output practice. I am getting the correct output for the first two input lines. But I am getting a zero for the third line input.
The given task is: To calculate the sum of some integers.
Input:
4 1 2 3 4
5 1 2 3 4 5
0
Output:
10
15
#include<stdio.h>
int main()
{
int i, first, next, total;
while(scanf("%d", &first) != EOF)
{
total = 0;
for(i = 1; i <= first; i++)
{
scanf("%d", &next);
total += next;
}
printf("%d\n", total);
if(first == 0)
{
printf(" ");
}
}
return 0;
}
If you do not want output when the first number on a line is zero, then you should test first == 0 before calculating and printing a total and break from the loop (break; if you want to stop the loop) or continue to the next iteration (continue;).
I'm trying to write C code where the user inputs 10 values and the index, and the output should display the index's value from the 10 values set by the user.
The problem statement:
Your grandparents gave you a fantastic cooking recipe but you can never remember how much of each ingredient you have to use! There are 10 ingredients in the recipe and the quantities needed for each of them are given as input (in grams). Your program must read 10 integers (the quantities needed for each of the ingredients, in order) and store them in an array. It should then read an integer which represents an ingredient's ID number (between 0 and 9), and output the corresponding quantity
Example Input:
500 180 650 25 666 42 421 1 370 211
3
My code:
#include <stdio.h>
int main(){
int ingred[9];
int readValue = 0;
int ID;
for(int i = 0; i < 9;i++){
scanf("%d %d", &readValue,&ID);
ingred[i] = readValue;
}
printf("%d",ingred[ID]);
return 0;
}
My output is always 0. Doesn't the scanf() function read the next line of code after the user presses "enter"? Please help.
Is it a requirement to read all of the quantities from a single line? Because that can be a little tricky to perform with scanf.
You could iterate trough a loop and get all of the quantities one by one, like this:
#include <stdio.h>
int main ()
{
int ingredients [10];
int newQuantity = 0;
int ingredientId;
int index;
for (index = 0; index < 10; index++)
{
printf ("Enter quantity #%d: ", index);
scanf ("%d", &newQuantity);
ingredients [index] = newQuantity;
}
printf ("Enter the ID: ");
scanf ("%d", &ingredientId);
printf ("Quantity: %d\n", ingredients [ingredientId]);
return 0;
}
On your code you tried to read a pair of integer values nine times in your loop. That's also something to note: despite array indexes beginning at 0, when you declare an array, the value between brackets is the number of elements of the array, not the index of the last element.
#include <stdio.h>
int main(void){
int i = 0;
int entry;
int index = 0;
int array[10];
for(i = 0; i <10; i++){
scanf("%d", &entry);
array[index] = entry;
index = index + 1;
}
scanf("%d", &index);
printf("%d", array[index]);
return 0;
}
Try this.