I'm trying to do a specific shape in c.
I have to print line 1-4 again in lines 6-9 like that :
Total 9 lines :
line 6 same like line 4.
line 7 same like line 3.
line 8 same like line 2.
line 9 same like line 1.
Is there a way for doing it without back every line again?
#include <stdio.h>
int
main ()
{
int s, i, j;
// for star
printf ("Enter size (10-20)\n");
scanf (" %d", &s);
for (i = 1; i < 2; i++)
{
for (j = 1; j <= 2 * s - 1; j++)
{
if (j == s)
printf ("o");
else
printf ("-");
}
printf ("\n");
}
for (i = 2; i < 3; i++)
{
for (j = 1; j <= 2 * s - 1; j++)
{
if (j == s + 1 || j == s - 1)
printf ("o");
else
printf ("-");
}
printf ("\n");
}
for (i = 3; i < 4; i++)
{
for (j = 1; j <= 2 * s - 1; j++)
{
printf ("o");
}
printf ("\n");
}
for (i = 4; i < 5; i++)
{
for (j = 1; j <= 2 * s - 1; j++)
{
if (j == 2 * s - 2 || j == 2)
printf ("o");
else
printf ("-");
}
printf ("\n");
}
for (i = 5; i < 6; i++)
{
for (j = 1; j <= 2 * s - 1; j++)
{
if (j == 2 * s - 3 || j == 3)
printf ("o");
else
printf ("-");
}
printf ("\n");
}
// line 6 same like line 4
// line 7 same like line 3
// line 8 same like line 2
// line 9 same like line 1
}
Thanks!
enter image description here
It has to be like that in the end.
thanks guys i did it with functions.
You don't need all the loops - you can just use strcpy and strcat. And if you store the results in different buffers you can use them more than once.
#include <stdio.h>
#include <string.h>
int main()
{
int size;
char line1[21];
char line2[21];
char line3[21];
char line4[21];
char line5[21];
printf ("Enter size (10-20)\n");
scanf ("%d", &size);
strncpy(line1,"---------------------",size);
strcat(line1,"o");
strncat(line1,"---------------------",size);
strncpy(line2,"---------------------",size-1);
strcat(line2,"o-o");
strncat(line2,"---------------------",size-1);
strncpy(line3,"ooooooooooooooooooooo",2*size+1);
strcpy(line4,"-o");
strncat(line4,"---------------------",2*size-3);
strcat(line4,"o-");
strcpy(line5,"--o");
strncat(line5,"---------------------",2*size-5);
strcat(line5,"o--");
puts(line1);
puts(line2);
puts(line3);
puts(line4);
puts(line5);
puts(line4);
puts(line3);
puts(line2);
puts(line1);
return 0;
}
Try it here: https://onlinegdb.com/NUPh1gxA9
Related
I'm trying to write a loop function that returns the result of a computation given a positive integer nVal.
Given nVal, the function computes for 1 + 2 - 3 + 4 - 5 + ... + nVal. So for example if nVal = 4, the function returns the result of 1 + 2 - 3 + 4. The solution I made isn't looping nVal properly and I don't know how to fix it.
Any fixes that I can try?
Here's my code so far (Btw I'm using C language):
#include <stdio.h>
int getValue (nVal)
{
int i, nResult = 0;
for (i = nVal; i <= nVal; i++)
{
if (i % 2 == 0)
{
nResult += i;
}
else if (i % 2 == 1)
{
nResult -= i;
}
}
if (nVal == 1)
nResult +=i + 1;
return nResult;
}
int main ()
{
int nVal, nResult;
printf ("Enter n value: ");
scanf ("%d", &nVal);
nResult = getValue(nVal);
printf ("Result: %d", nResult);
return 0;
}
Because the numbers are consecutives,I removed the if elseif statement ,I use the variable k to reverse '+' to '-',my loop start from 2 to nVal
Your loop
for (i = nVal; i <= nVal; i++)
runs just 1 time
so ,you should changed it to
for (i = 2; i <= nVal; i++)
and (nResult=1)because in your exercise the sign changed from the third number, not from the second number
here:
if (nVal == 1)
nResult +=i + 1;
If I write as 1 input ,the output is 2 ,I remove this line
my code:
#include <stdio.h>
int getValue (nVal)
{
int i, nResult = 1;
int k=1;
for (i = 2; i <= nVal; i++)
{
nResult+=i*k;
k*=-1;
}
return nResult;
}
int main ()
{
int nVal, nResult;
printf ("Enter n value: ");
scanf ("%d", &nVal);
nResult = getValue (nVal);
printf ("Result: %d", nResult);
return 0;
}
I want to print some C pattern for upper triagnle which sholud print numeric value 1 to 10.
Now my code is like this:-
#include <stdio.h>
int main() {
int j = 1, k, l, i;
for (i = 4; i >= 1; i--) {
for (k = i - 1; k >= 0; k--) {
printf(" ");
}
for (l = 4; l >= i; l--) {
printf("%2d", j);
j++;
}
printf("\n");
}
return 0;
}
Its output is fine but when my value reach near 10 it's space is missing.
How can I resolve it?
My output looks like
1
2 3
4 5 6
7 8 910
It should be:
1
2 3
4 5 6
7 8 9 10
You can change the alignment by using the - sign. This makes it left-justified.
Replace
printf("%2d",j);
with
printf("%-2d",j);
Live demo
If you want the bottom of the pyramid to be completely to the left with no space you can replace
for (k = i - 1; k >= 0; k--)
with
for (k = i - 1; k > 0; k--)
EDIT:
As #chqrlie pointed out this will leave a trailing blank space in every line except the last one, this can be fixed like:
//...
for (i = 4; i >= 1; i--)
{
for (k = i - 1; k > 0; k--) // change from k >= 0 to k > 0
{
printf(" ");
}
for (l = 4; l >= i; l--)
{
printf(" %d", j); // change from "%2d" to " %d"
j++;
}
printf("\n");
}
//...
Live demo
For this problem, with n lines of output, you need to produce n - i spaces at the beginning of line i and i numbers each preceded by a space. Instead of "%2d", use " %d" and output one less space at the start of each line. You can use printf to output an arbitrary number of spaces using %*s and an empty string.
Here is a modified version:
#include <stdio.h>
int main() {
int i; // line number
int j = 1; // starting number
int n = 4; // number of lines
int k;
for (i = 1; i <= n; i++) {
printf("%*s", n - i, ""); // output n - i spaces
for (k = 0; k < i; k++) {
printf(" %d", j);
j++;
}
printf("\n");
}
return 0;
}
Output:
1
2 3
4 5 6
7 8 9 10
It can be solved in two ways:
One way to resolve this is by simply add spacing (padding) for each character you are printing.
#include<stdio.h>
int main()
{
int j=1,k,l,i;
for(i=4;i>=1;i--){
for(k=i-1;k>=0;k--){
printf(" ");
}
for(l=4;l>=i;l--){
printf(" %2d ",j); //Padding added
^ ^
j++;
}
printf("\n");
}
return 0;
}
Secondly you can change the alignment by using - sign
#include<stdio.h>
int main()
{
int j=1,k,l,i;
for(i=4;i>=1;i--){
for(k=i-1;k>=0;k--){
printf(" ");
}
for(l=4;l>=i;l--){
printf("-%2d",j); //Left justified
^
j++;
}
printf("\n");
}
return 0;
}
Amongst the other good answers, another way is to use printf("%d ", j); instead of printf("%2d", j); - Note the white space ' ' behind the %d format specifier. - This method has the disadvantage that you have a trailing white space after 10 but it accomplishes the (obvious) desired output:
Online example
#include <stdio.h>
int main (void)
{
int j = 1, k, l, i;
for (i = 4; i >= 1; i--){
for (k = i - 1; k >= 0; k--) {
printf(" ");
}
for(l = 4; l >= i; l--) {
printf("%d ", j);
j++;
}
printf("\n");
}
return 0;
}
Output:
1
2 3
4 5 6
7 8 9 10
I have an array with 100 numbers in it, and I am trying to print it out with only 10 ints on each line, and a tab between each number. It is only printing the first 10 integers and then stopping, which makes sense because of my for loop. I am clearly missing part of it to allow for it to continue through the array. I was going to try to add the line
for(int line_num = 0; line_num < 10; line_num+=10)
before the for statement after the while loop
int array_value;
int length_of_array = 100;
while (length_of_array <= 100){
for(array_value = 0; array_value < 10; ++array_value){
printf("%d ", A[array_value]);
++length_of_array;
}
I was also thinking of including a line like
if (array_value % 10 == 0)
printf("\n");
I figured it out! Posted the answer below.
This might be what you're looking for:
/* test.c */
#include <stdio.h>
#define ELEMENTS 100
int main (void)
{
int array [ELEMENTS];
for ( int i = 0; i < ELEMENTS; ++i )
array [i] = i;
for ( int i = 0; i < ELEMENTS; ++i ) {
printf ("%i", array[i]);
if ( (i + 1) % 10 != 0 )
printf ("\t");
else
printf ("\n");
}
return 0;
}
edit: Because of the way the tab can extend to the next line at the end of the line you have to be careful with the tab and new line character.
For clarity, rename length_of_array to offset_in_array and then set it to zero at the start. I renamed array_value and corrected your length check. I also added a check to the inner loop in case the array length gets changed and doesn't divide by 10.
Something like:
int i;
#define ARRAY_LENGTH 100
int offset_in_array = 0;
while (offset_in_array < ARRAY_LENGTH){
for(i = 0; i < 10 && offset_in_array < ARRAY_LENGTH; ++i){
printf("%d ", A[offset_in_array]);
++offset_in_array;
}
}
I haven't tried running this but it should be closer.
Just print a newline every tenth number... If it's not a tenth number, then print a tab.
for (size_t i = 0; i < array_length; ++i) {
printf("%d%c", A[i], i % 10 != 9 ? '\t' : '\n');
}
Live code available at onlinedbg.
Just change the value of length_of_array to 0 and print \n after a for loop.
int array_value;
int length_of_array = 0;
while (length_of_array <= 100) {
for(array_value = 0; array_value < 10; ++array_value){
printf("%d ", A[array_value]);
++length_of_array;
}
printf("\n");
}
You can use the following solution to print 10 lines of 100 array values in C:
for (int i = 0; i < 100; ++i){
printf("%i\t", A[i]);
if ((i+1)%10 == 0){
printf("\n");
}
}
I have problem with my code I need to make. I have to take 14 parameters from command line and use them to make lottery numbers, winning numbers and then compare those 2 with each other.
For example using this parameter: ./a.out 2 30 17 8 6 19 24 7 6 1 2 3 5 4
Should make something like this:
Winning numbers: 2 30 17 8 6 19 24
Lottonumbers: 7 6 1 2 3 5 4
2 are the same: 6 2
My code is almost working as intended, but I can't seem to print this right: 2 are the same. It always loops like this: 1 are the same: 6 2 are the same: 2.
Number 2 is the amount of same numbers that are found when 2 arrays are compared. My question is how can I print it so that it won't duplicate the text and with the right amount? My head can't seem to work even if it's so simple :/
#include <stdio.h>
#include <stdlib.h>
int main(int args, char **argv)
{
int i;
int winningNumbers[7];
int lottoNumbers[7];
int j;
int a;
int b;
int winningNumber;
int lottoNumber;
int count = 0;
printf("Winning numbers: ");
for (i=0;i<7; i++) {
winningNumber = atoi(argv[i+1]);
winningNumbers[i] = winningNumber;
printf("%d ", winningNumber);
}
printf("\n");
printf("Lotto numbers:: ");
for (j= 8; j < args; j++) {
lottoNumber = atoi(argv[j]);
lottoNumbers[j-8] = lottoNumber;
printf("%d ", lottoNumber);
}
printf("\n");
for(a = 0; a < 7; a++) {
for(b=0; b < 7; b++) {
if (lottoNumbers[a] == winningNumbers[b]) {
count = count + 1;
printf("%d are the same: %d", count, winningNumbers[b]);
}
}
}
return 0;
}
Searching for matches and displaying the result are two separate tasks. It is simpler and more flexible not to attempt to do them at the same time.
First search for the matches and store them in an array. Then display the content of the array however you want.
int main (int argc, char *argv[])
{
int winningNumbers[7];
int lottoNumbers[7];
int commonNumbers[7];
int count = 0;
// fill winningNumbers
// fill lottoNumbers
// NOTE: the following loop assumes that in both arrays
// no number is repeated.
// You should check that this is indeed the case.
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if (lottoNumbers[i] == winningNumbers[j]) {
commonNumbers[count] = lottoNumbers[i];
count++;
}
}
}
printf ("%d are the same:", count);
for (int i = 0; i < count; i++) {
printf (" %d", commonNumbers[i]);
}
printf ("\n");
return 0;
}
Many simple programs should follow this structure:
read and check input
transform input to output
print output
for(b=0; b < 7; b++) {
if (lottoNumbers[a] == winningNumbers[b]) {
count = count + 1;
}
}
printf("%d are the same: ", count);
for(b=0; b < 7; b++) {
if (lottoNumbers[a] == winningNumbers[b]) {
printf(" %d", winningNumbers[b]);
}
}
printf("\n");
int finalArray[7];
int i;
for(a = 0; a < 7; a++) {
for(b=0; b < 7; b++) {
if (lottoNumbers[a] == winningNumbers[b]) {
finalArray[count] = lottoNumbers[a];
count = count + 1;
}
}
}
printf("%d are same: ", count);
for(i = 0; i < count; i++)
printf("%d ", finalArray[i]);
I'm writing a program that reads integers from keyboard input and find the occurrence then sort them in descending order.
I got the occurrence and the descending but when I type same integers, they are repeated if I type 8 7 8
it's like
8 2
8 2
7 1
help please??
This is my code
#pragma warning (disable :4996)
#include <stdio.h>
#include <stdlib.h>
int main(){
int input;
int inputarr[50], count[50] = {0};
int i=0, j;
int last = 0;
printf("Enter numbers \n");
///getting int
while (scanf("%d", &input) > 0)
{
inputarr[i] = input;
i++;
}
last = i;
printf(" N Count\n");
printf("----- -----\n");
int a;
/// increment count
for (i = 0; i < last; i++){
count[inputarr[i]] = count[inputarr[i]] + 1;
}
/////ascending
for (i = 0; i < last; i++)
{
for (j = 0; j < last; j++){
if (inputarr[j]<inputarr[j + 1])
{
int temp = inputarr[j];
inputarr[j] = inputarr[j + 1];
inputarr[j + 1] = temp;
}
}
printf(" %d %d\n", inputarr[i], count[inputarr[i]]);
}
return 0;
}
The problem with this code is that you are printing the array while it's being sorted. Inputing numbers 1 2 3 4 would result in numbers 2 1 4 1 2 1 1 1 being outputed. To accomplish what you wanted you should move the printing part out of the sorting loop. Even then when a number is appearing multiple times in the input the output won't contain it only once (ex. 8 7 8 -> 8 2 8 2 7 2). To do that you should not output the number if it is the same as the previous number you outputed.
Another thing, in the sorting loop you are potentially accessing non existing array elements at the line
for (j = 0; j < last; j++){
When j = last-1 you are accessing last element which might not exsist.
After fixing all this problems code might look like this:
for (i = 0; i < last; i++)
{
for (j = 0; j < last-1; j++){
if (inputarr[j]<inputarr[j + 1])
{
int temp = inputarr[j];
inputarr[j] = inputarr[j + 1];
inputarr[j + 1] = temp;
}
}
}
if (last == 0)
return 0;
printf(" %d %d\n", inputarr[0], count[inputarr[0]]);
for(i = 1; i < last; i++)
if (inputarr[i] != inputarr[i-1])
printf(" %d %d\n", inputarr[i], count[inputarr[i]]);