I want to create a CountDownUP program but the code is not working, I want it to output 5 4 3 2 1 0 1 2 3 4 5 but it only outputs 5 4 3 2 1 0 1
void countDownUp (unsigned int k){
printf("\n");
for (int i = 0; i < k; i=1){
k = k - i;
printf("%d ", k);
}
for (int n = 0; n <= k; n++){
printf("%d ",n);
}
}
int main(){
int num;
fflush(stdout);
scanf("%d", &num);
countDownUp (num);
return 0;
}
The first for loop decrements k each time through the loop, and stops when k == 1. So the second loop just iterates from 0 to 1.
You shouldn't modify k in the first loop, you should decrement i.
for (int i = k; i > 1; i--) {
printf("%d ", i);
}
Just for fun, you can do it using one for with:
for(int i = k; i >= -k; i--)
if(i <= -1)
printf("%d ", -i);
else
printf("%d ", i);
Related
the program should this:
the matrix use 1,2,...,n on first line and 2,3,...n,n-1 on second etc, for instance :
input :
5
expected output:
1 2 3 4 5
2 3 4 5 4
3 4 5 4 3
4 5 4 3 2
5 4 3 2 1
i get try maybe anyone can help me to solve this program.
this my program before:
#include <stdio.h>
void makeSymmetricMatrix(int n) {
int i,j;
int matrix[n][n];
for( i = 0; i<n; i++){
int count = 1;
for( j = 0; j <n; j++){
if(i == j){
matrix[i][j] = 0;
}else{
matrix[i][j] = count++;
}
}
}
for( i = 0; i<n; i++){
for( j = 0; j <n; j++){
printf("%d", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int n = 5;
makeSymmetricMatrix(n);
}
I really need your correction about my program
Try this function.
void makeSymmetricMatrix(int n)
{
int i, j, count, decrease;
int matrix[n][n];
for(i = 0; i < n; i++)
{
decrease = 0;
count = i+1;
for(j = 0; j < n; j++)
{
matrix[i][j] = count;
if(count == n)
decrease = 1;
if(decrease == 1)
count--;
else
count++;
}
}
for( i = 0; i<n; i++){
for( j = 0; j <n; j++){
printf("%d", matrix[i][j]);
}
printf("\n");
}
}
Basically it stores a variable decrease which gets triggered when count reaches the maximum value (n here). Once that happens, numbers are stored in a reverse order.
Result for n = 6:
123456
234565
345654
456543
565432
654321
can be done using :
void makeSymmetricMatrix(int n) {
int i,j;
int matrix[n][n];
for (i = 0; i<n; i++) {
int v = i+1, offset = 1;
for (j = 0; j <n; j++){
matrix[i][j] = v;
if (v == n)
offset = -1;
v += offset;
}
}
for( i = 0; i<n; i++){
for( j = 0; j <n; j++){
printf("%d ", matrix[i][j]);
}
putchar('\n');
}
}
in each line the first value equals the rank of the line plus 1 and by default the next value is the current plus 1
each time the value reaches n the rest of the values of the same line are the previous minus 1
After the change compilation and execution :
pi#raspberrypi:/tmp $ gcc -Wall m.c
pi#raspberrypi:/tmp $ ./a.out
1 2 3 4 5
2 3 4 5 4
3 4 5 4 3
4 5 4 3 2
5 4 3 2 1
pi#raspberrypi:/tmp $
i have to do.
instruction:
Write a C program which reads a number n from the user and in decreasing order
prints the numbers between 1 and n (included) that are divided by 3 and at the same time
are not divided by 2 and 5. Print also their summation.
if n is 100
expected output:
int main(void)
{
int i, j;
printf("Enter a number: ");
scanf("%d", &i);
for(j = i; j > 0; j--)
if (j % 3 == 0)
if (j % 2 != 0)
if (j % 5 != 0)
printf("%d ", j);
return 0;
}
i have to sum all of them.
Your code produces the correct output, but does not compute the sum.
It is easy to do this: add a variable sum initialized to 0 and increment it by the numbers that you print.
Here is a modified version:
#include <stdio.h>
int main(void) {
int i, j;
long sum = 0; // use type long to avoid overflow on large values.
printf("Enter a number: ");
if (scanf("%d", &i) == 1) {
for (j = i; j > 0; j--) {
if (j % 3 == 0) {
if (j % 2 != 0) {
if (j % 5 != 0) {
printf("%d ", j);
sum += j;
}
}
}
}
printf("\n\nthe sum is %ld\n", sum);
}
return 0;
}
Output:
Enter a number: 100
99 93 87 81 69 63 57 51 39 33 27 21 9 3
the sum is 732
Note that the nested if statements can be combined into a single test expression:
if (j % 3 == 0 && j % 2 != 0 && j % 5 != 0)
Here is a more obscure version with a single modulo operator per iteration and a lot less of them:
#include <stdio.h>
int main(void) {
int i, j;
long sum = 0; // use type long to avoid overflow on large values.
printf("Enter a number: ");
if (scanf("%d", &i) == 1) {
for (j = i / 3; j > 0; j--) {
if (650 >> j % 10 & 1) {
printf("%d ", j * 3);
sum += j * 3;
}
}
printf("\n\nthe sum is %ld\n", sum);
}
return 0;
}
my code works thanks for the all answers. i promise i will try harder before ask a question. Thanks for every little help.
#include <stdio.h>
int main(void)
{
int i, j;
long sum = 0;
printf("Enter a number: ");
scanf("%d", &i);
for(j =i; j >0; j--)
if (j % 3 == 0)
if (j % 2 != 0)
if (j % 5 != 0)
{
printf("%d ", j);sum += j;
}
printf ("\n\nthe sum is: %ld\n", sum);
return 0;
}
hey look at this code may help you
int main(void){int i, j;printf("Enter a number: ");scanf("%d", &i);for(j = i; j > 0; j--)
if (j % 3 == 0 || j % 2 != 0|| j % 5 != 0) printf("%d", j);
return 0;}
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
I'm having a problem with matrix.
When i'm trying to print the last matrix ( Invertible matrix ) to file ( matrixrez.txt) program prints only first column.
Why?
Matrix.txt:
1 0 1 1 2 0
1 0 2 0 3 0
2 1 1 1 2 3
Here's my code:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file, *outf;
int matrixA[3][3], matrixB[3][3], a[3][3];
int trash[3];
int i, j, k, sum;
float determinant = 0;
i = j = k = sum = 0;
file = fopen("matrix.txt", "rt");
outf = fopen("matrixrez.txt", "w+");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
fscanf(file, "%d", &matrixA[i][j]);
}
for (k = 0; k < 3; k++) fscanf(file, "%d", &trash[k]);
}
fseek(file, 0, SEEK_SET);
for (i = 0; i < 3; i++) {
for (k = 0; k < 3; k++) fscanf(file, "%d", &trash[k]);
for (j = 0; j < 3; j++) {
fscanf(file, "%d", &matrixB[i][j]);
}
}
/* Matrix multiplication */
for ( i = 0 ; i < 3 ; i++ )
{
for ( j = 0 ; j < 3 ; j++ )
{
for ( k = 0 ; k < 3 ; k++ )
{
sum = sum + matrixA[i][k]*matrixB[k][j];
}
a[i][j] = sum;
sum = 0;
}
}
for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf("%d ", matrixA[i][j]);
}
}
printf("\n");
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf("%d ", matrixB[i][j]);
}
}
printf("\n");
printf("The resultant matrix is:: \n");
fprintf(outf,"The resultant matrix is:: \n");
for (i = 0; i < 3; i++) {
printf("\n");
fprintf(outf,"\n");
for (j = 0; j < 3; j++) {
printf("%d ", a[i][j]);
fprintf(outf,"%d ", a[i][j]);
}
}
printf("\n\n Inversion of Matrix: \n");
fprintf(outf,"\n\n Inversion of Matrix: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3] [(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"\n");
printf("\n");
}
printf("\n");
system("pause");
return 0;
}
I get matrixrez.txt:
The resultant matrix is::
2 4 3
3 6 6
3 9 3
Inversion of Matrix:
4.00
-1.67
-0.67
I think something wrong is with line at the end of a file (if I'm not mistaken) :
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
You need to put braces around what you meant to have as your for loop block at the end:
printf("\n\n Inversion of Matrix: \n");
fprintf(outf,"\n\n Inversion of Matrix: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
{ /* YOU NEED THIS BRACE */
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3] [(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
} /* AND YOU NEED THIS BRACE */
fprintf(outf,"\n");
printf("\n");
}
You do not have brackets around the inner for loop. So, it loops through i just fine but each time it cycles through j without calling the fprintf just the printf
The code should be
printf("\n\n Inversion of Matrix: \n");
fprintf(outf,"\n\n Inversion of Matrix: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3] [(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"\n");
printf("\n");
}
}
How in the world do you format this for things to line up, ive tried everything, nothing changes?!
printf("N Count\n");
printf("---- ------ \n");
for(i = 0 ; i < MAX_ELEMENTS ; i++)
{
count = getOccur(arr, MAX_ELEMENTS, arr[i]);
printf("%1d %1d\n", arr[i], count);
}
I've tried tabbing, spacing, those % signs with the numbers for the last one, it wont change from this
N Count
----- ---
1 1
2 1
3 1
Driving me crazy! I dont get it! lol
EDIT WHOLE PROGRAM NEW QUESTION!
#include <stdio.h>
#define MAX_ELEMENTS 10
int getOccur(int a[], int num_elements, int value);
void printArr(int a[], int num_elements);
int main()
{
int arr[MAX_ELEMENTS];
int trim[MAX_ELEMENTS];
int count, target, i;
int j, k, temp;
for(i = 0 ; i < MAX_ELEMENTS ; i++)
{
printf("Enter a variable for the array: ");
scanf("%d", &arr[i]);
}
for (j = 1 ; j <= MAX_ELEMENTS-1 ; j++)
{
for(k = 0 ; k <= MAX_ELEMENTS-2 ; k++)
{
if(arr[k] > arr[k+1])
{
temp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = temp;
}
}
}
printf("%4s %6s\n", " N ", "Count");
printf("%4s %6s\n", "----", "------");
for(i = 0 ; i < MAX_ELEMENTS ; i++)
{
count = getOccur(arr, MAX_ELEMENTS, arr[i]);
printf("%3d %4d\n", arr[i], count);
}
}
int getOccur(int a[], int tally, int value)
{
int i;
tally = 0;
for( i = 0 ; i < MAX_ELEMENTS ; i++)
{
if (a[i] == value)
{
++tally;
}
}
return(tally);
}
void printArr(int a[], int amount)
{
int i;
for(i = 0 ; i < amount ; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
printf("%4s %6s\n", " N ", "Count");
printf("%4s %6s\n", "----", "------");
for(i = 0 ; i < MAX_ELEMENTS ; i++)
{
count = getOccur(arr, MAX_ELEMENTS, arr[i]);
printf("%4d %6d\n", arr[i], count);
}
That should line everything up.
EDIT
In response to the question in the comments, my approach would be to first find all the unique values in arr and save them to a different array (call it unique). Then you'd walk through the unique array in your loop:
for (i = 0; i < uniqueCount; i++)
{
count = getOccur(arr, MAX_ELEMENTS, unique[i]);
printf("%4d %6d\n", unique[i], count);
}
As for finding unique elements, the brute force method would be something like
size_t uniqueCount = 0;
int unique[MAX_SIZE]; // needs to be same size as original array in case
// all values are unique
for (i = 0; i < MAX_SIZE; i++)
{
size_t j = 0;
for (j = 0; j < uniqueCount; j++)
if (arr[i] == unique[j])
break;
if (j == uniqueCount)
unique[uniqueCount++] = arr[i];
}
For each element in the original array, we scan the (initially empty) unique array. If we don't find the value of a[i] in the unique array, we add it and increment uniqueCount.
Note that this method is pretty inefficient and will perform poorly as MAX_ELEMENTS gets large. Better solutions are available, but you sound like you're still at the bottom of the learning curve, so I'll leave it at that.
This is what you are looking for. The first column you have four - so the minimum width is four, the next column has size - so minimum width is 6 as specified in the format string. see printf manual page
printf("N Count\n");
printf("---- ------ \n");
for(i = 0 ; i < MAX_ELEMENTS ; i++)
{
count = getOccur(arr, MAX_ELEMENTS, arr[i]);
printf("%4d %6d\n", arr[i], count);
}
EDIT
Edited first printf