I have just learnt about arrays. I was trying to create a database program using arrays. It's a very basic program.
#include<stdio.h>
#define N 1 //number of entries needed
int main()
{
int i, k = 1, l = 1, w, x = 0, y = 0;
int rollnum[N], hsc[N], cet[N], a[N], b[N];
char name[100], city[100], c;
for(i = 0; i < N; i++)
{
printf("%d.\n", (i+1));
printf("Enter first name : ");
do
{
c = getchar();
if(c != '\n')
{
name[k] = c;
k++;
}
}
while(c != '\n');
a[i] = k;
k++;
printf("\n");
printf("Enter roll number : ");
scanf("%d", &rollnum[i]);
printf("\n");
getchar();
printf("Enter city : ");
do
{
c = getchar();
if(c != '\n')
{
city[l] = c;
l++;
}
}
while(c != '\n');
b[i] = l;
l++;
printf("\n");
printf("Enter HSC percentage : ");
scanf("%d", &hsc[i]);
printf("\n");
printf("Enter CET marks : ");
scanf("%d", &cet[i]);
printf("\n");
getchar();
}
printf("\n\n\n");
k = 1;
l = 1;
for(i = 0; i < N; i++)
{
printf("Entry %d\n", (i+1));
printf("Student Name : ");
x = (a[i] - x);
for(w = 0; w < x; w++ && k++)
putchar(name[k]);
putchar('\n');
printf("Roll number : %d", rollnum[i]);
printf("\n");
printf("City : ");
y = (b[i] - y);
for(w = 0; w < y; w++ && l++)
putchar(city[l]);
putchar('\n');
printf("Marks : \n");
printf("\t");
printf("HSC : %d ", hsc[i]);
printf("\t");
printf("CET : %d / 200", cet[i]);
printf("\n\n\n");
}
return 0;
}
The program is not functioning the way I want it to! When I enter a name, the first letter is being printed out twice, same is the case with city! If I put 2 entries by modifying my 'N' , I'm getting the first letters of name and address(of second entry) as garbage values . I don't think there is any error in my logic, because i tried doing it manually in my notebook and I did not find any fault in it.
Can anyone help me find the mistake? I know the program might not at all be good and efficient, but I'm just trying out stuff that I've learnt!
You have made some mistakes initializing some variables. Also you don't have to read a string one character at a time. You can use scanf("%s", someString) to read a whole string.
And here is a working code that looks a lot more cleaner:
#include<stdio.h>
#define N 2
int main()
{
int rollnum[N], hsc[N], cet[N], i;
char name[100][100], city[100][100];
for(i = 0; i < N; i++)
{
printf("%d.\nEnter first name : ", (i+1));
scanf("%s", name[i]);
printf("\nEnter roll number : ");
scanf("%d", &rollnum[i]);
printf("\nEnter city : ");
scanf("%s", city[i]);
printf("\nEnter HSC percentage : ");
scanf("%d", &hsc[i]);
printf("\nEnter CET marks : ");
scanf("%d", &cet[i]);
printf("\n");
}
printf("\n\n\n");
for(i = 0; i < N; i++)
{
printf("Entry %d\nStudent Name : %s\nRoll number : %d\nCity : %s\nMarks : \n\tHSC : %d \tCET : %d / 200\n\n\n", (i+1), name[i], rollnum[i], city[i], hsc[i], cet[i]);
}
return 0;
}
It works for multiple entries too.
In order to make your program work you have to replace this:
x = (a[i] - x);
for(w = 0; w < x; w++ && k++)
putchar(name[k]);
putchar('\n');
printf("Roll number : %d", rollnum[i]);
printf("\n");
printf("City : ");
y = (b[i] - y);
for(w = 0; w < y; w++ && l++)
putchar(city[l]);
with this:
if (i == 0)
x = a[i]-1;
else
x = a[i] - a[i-1];
for(w = 0; w < x; w++)
putchar(name[k++]);
putchar('\n');
printf("Roll number : %d", rollnum[i]);
printf("\n");
printf("City : ");
if (i == 0)
y = b[i]-1;
else
y = b[i] - b[i-1];
for(w = 0; w < y; w++)
putchar(city[l++]);
the problem was that you didn't calculate the length of the words correctly, and also another error that I still can't explain but I removed that to.
Related
I'm trying to allow the user to only write names of students in uppercase in C.
I've tried :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {
int number_ofstudents, i, j;
char Name[100][20];
do {
printf("Enter number of students \n");
scanf("%i", &number_ofstudents);
} while ((number_ofstudents < 0) || (number_ofstudents > 100));
for (i = 0; i < number_ofstudents; i++) {
for (j = 0; j <= strlen(Name[i]) - 1; j++) {
printf("Enter the name of student number %i in uppercase \n", i + 1);
do {
fflush(stdin);
fgets(Name[i], 20, stdin);
} while ((Name[i][j] < 'A') || (Name[i][j] > 'Z'));
}
}
return 0;
}
I've also tried:
for (i = 0; i < number_ofstudents; i++) {
for (j = 0; j <= Name[i][j] != '\0'; j++) {
printf("Enter the name of student number %i in uppercase \n", i + 1);
do {
fflush(stdin);
fgets(Name[i], 20, stdin);
} while ((Name[i][j] < 'A') || (Name[i][j] > 'Z'));
}
}
What I'm expecting is for the program to keep demanding that I enter the name of the student number x until my input is all in uppercase, however once I execute none of my inputs are accepted and the phrase "Enter the name of student number 1 in uppercase " keeps repeating itself.
Does the issue lie in my 2nd for loop when I'm trying to manipulate the letter input? I haven't learned pointers or functions yet.
I need to make a program that stores numbers inside of an array. But it must have no duplicate elements.
int x;
int z[8];
for( x = 0; x<8;x++)
printf("number: ");
scanf("%d",&z[x]);
}
for( x=0;x<8;x++) {
printf("%d ",z[x]);
}
First, initialize the array, so that you do not end up reading an uninitialized value and fail the test.
int user_nums[6] = {0};
Next, you need to have another check in the for loop, to read the number again if it is a duplicate.
The code will look like this.
#include<stdio.h>
int main(){
int x,y;
int exists = 0;
int user_nums[6] = {0};
for( x = 0; x<6;x++){//for loop to get the players selected numbers
do {
exists = 0;
printf("Enter a number(from the #'s 1-42): ");
scanf("%d",&user_nums[x]);
for(y =0; y < x; y++) { //to check for duplicates
if (user_nums[x] == user_nums[y])
{
printf("Number already exists\n ");
exists = 1;
break;
}
}
}while (user_nums[x]<1 || user_nums[x]>42 || exists);//accepts only numbers from 1-42 which are not duplicates (continous to ask you for a number until condition is met).
}
printf("Your numbers: \n");
for( x=0;x<6;x++){
printf("%d ",user_nums[x]); // prints the numbers you inputed.
}
return 0;
}
The following code could work in O(n):
#include<stdio.h>
int main()
{
int user_nums[6];
int index[50];
for (int i = 0; i != sizeof(index) / sizeof(index[0]); ++i)
index[i] = -1;
for (int i = 0; i < sizeof(user_nums) / sizeof(user_nums[0]); ++i) {
for (;;) {
printf("Enter a number(from the #'s 1-42): ");
scanf("%d", user_nums + i);
if (user_nums[i] < 1 || user_nums[i] > 42) {
printf("wrong number\n");
continue;
}
if (index[user_nums[i]] != -1) {
printf("dump number\n");
continue;
}
index[user_nums[i]] = i;
break;
}
}
printf("Your numbers: \n");
for(int i = 0; i < 6; ++i)
printf("%d ", user_nums[i]);
return 0;
}
I am unable to get this program to print out the lines of symbols with a tab. I included a picture of how it should be printing. Currently it's working except that there is no indentation. Any help at all would be greatly appreciated.
The idea is to display even numbered lines with indents (â\tâ) and the odd ones without indent:
Example of correct output
#include <stdio.h>
int main(void) {
int num_lines;
printf("Enter a number of lines, greater than or equal to 7, to print : ");
scanf("%d", &num_lines);
if (num_lines < 7) {
while ( num_lines < 7 ) {
printf("Enter the number of lines to print\nMust be greater than or equal to 7 : ");
scanf("%d", &num_lines);
}
}
char symbol;
printf("Choose a symbol/character to be displayed */&/+/0/x : ");
scanf(" %c", &symbol);
int num_symbols;
printf("Enter the number of symbols to print per line : ");
scanf("%d", &num_symbols);
if (num_symbols < 7 || num_symbols > 27) {
num_symbols = 19;
}
while (num_lines > 0) {
int n = num_symbols;
int nl = 1;
int nll = nl / 2;
while (nl <= num_lines) {
if ( (nl % 2) == 0) {
while (nll > 0) {
printf("\t");
--nll;
}
while (n > 0) {
printf("%c", symbol);
--n;
}
}
else {
while (n > 0) {
printf("%c", symbol);
--n;
}
}
++nl;
}
printf("\n");
--num_lines;
}
return;
}
In your loop there are problems in the way you declare your variables and put your conditions.
Try to think step by step:
1. Am I on on a pair or impair line?
2. If yes I put \t
3. Print x times my number
4. Do the same until you've done enough lines
if (num_symbols < 7 || num_symbols > 27) {
}
int which_line = 1;
while (num_lines > 0) {
if (which_line % 2 == 0) { //STEP 1
for (int tmp_line = which_line - 1; tmp_line >= 1 ; tmp_line--) { //STEP2
printf("\t");
}
}
for (int tmp_symbols = num_symbols; tmp_symbols >= 1 ; tmp_symbols--) { //STEP 3
printf("%c", symbol);
}
printf("\n");
which_line++; //STEP 4
--num_lines;
}
You can try this to print your desired pattern :
#include<stdio.h>
int main(void) {
int num_lines,i,j;
printf("Enter a number of lines, greater than or equal to 7, to print : ");
scanf("%d", &num_lines);
while ( num_lines < 7 ) {
printf("Enter the number of lines to print\nMust be greater than or equal to 7 : ");
scanf("%d", &num_lines);
}
char symbol ;
printf("Choose a symbol/character to be displayed */&/+/0/x : ");
scanf(" %c", &symbol);
int num_symbols;
printf("Enter the number of symbols to print per line : ");
scanf("%d", &num_symbols);
if (num_symbols < 7 || num_symbols > 27) {
num_symbols = 19;
}
i = 1;
while (i <= num_lines) {
if(i%2 == 0){
j = i/2;
while(j != 0){
printf("\t");
j--;
}
}
j = 0;
while(j < num_symbols){
printf("%c",symbol);
j++;
}
printf("\n");
i++;
}
return 0;
}
^_^
#include <stdio.h>
int main(void) {
int num_lines;
do{
printf("Enter the number of lines to print\nMust be greater than or equal to 7 : ");
scanf("%d", &num_lines);
}while(num_lines < 7);
char symbol;
printf("Choose a symbol/character to be displayed */&/+/0/x : ");
scanf(" %c", &symbol);
int num_symbols;
printf("Enter the number of symbols to print per line : ");
scanf("%d", &num_symbols);
if (num_symbols < 7 || num_symbols > 27) {
num_symbols = 19;
}
for (int i = 1; i <= num_lines; ++i) {
if (i % 2 == 0) {
for (int m = 0; m < i / 2; ++m) {
printf("\t");
}
}
for (int j = 0; j < num_symbols; ++j) {
printf("%c", symbol);
}
printf("\n");
}
return 0;
}
I got pretty stuck when I had a to do a letter pyramid in C (via Xcode). We have to input the number of rows which will be equivalent to the number of capital letters. This is what it should look like :
Enter number of rows : 4
Output:
A
ABA
ABCBA
ABCDCBA
It is limited to min: 4 and Max 26. This is what I've managed so far. Thank you in advance :
int main(){
char letter = 'A'; //NOTE ! letter + 1 = 'B'
int i, rows, space, k = 0;
printf("Enter number of rows: \t");
scanf("%i", &rows);
while(rows < 4 || rows > 26){
printf("\nError !\nMin : 4\tMax : 26\n");
scanf("%i", &rows);
}
for(i = 1; i <= rows; i++,k =0){
for(space = 1; space <= rows - i; space++){
printf(" ");
}
for(; k!= 2* i-1; k++){
printf("%c", letter);
}
for (char temp = letter + 1; temp <= letter; temp--){
printf("%c", temp);
}
printf("\n");
}
return 0;
}
Output:
Enter number of rows: 4
A
AAA
AAAAA
AAAAAAA
Program ended with exit code: 0
Solved :
#include <stdio.h>
int main(){
char letter = 'A', temp, temp1; //NOTE ! letter + 1 = 'B'
int i, rows, space, k = 0;
printf("Enter number of rows: \t");
scanf("%i", &rows);
while(rows < 4 || rows > 26){
printf("\nError !\nMin : 4\tMax : 26\n");
scanf("%i", &rows);
}
for(i = 1; i <= rows; i++,k =0){
for(space = 1; space <= rows - i; space++){
printf(" ");
}
temp = letter + i - 1;
temp1 = temp;
for(; k!= 2* i-1; k++){
if(letter == temp){
printf("%c", temp1);
temp1--;
}else{
printf("%c", letter);
letter++;
}
}
letter = 'A';
printf("\n");
}
return 0;
}
This is a homework problem. I have a C program that takes user input for a number of people's first names, last names, and ages. Right now it works and prints out the names to the console correctly, but it is not printing out the right ages, and I can't figure out what I'm doing wrong. Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int choice;
int i = 0;
int x,k,l;
fputs("How many people would you like to add? ", stdout);
scanf(" %d", &choice);
fflush(stdout);
int ch;
while((ch = getchar()) != EOF && ch != '\n');
if (ch == EOF)
{
}
char firstName[choice][20];
char lastName[choice][20];
int age[choice][3];
char first[20];
char last[20];
int a[3];
for (x = 0; x < choice; x++)
{
for (l = 0; l < 3; l++)
{
age[x][l] = 0;
a[l] = 0;
}
}
while(i < choice)
{
printf("Enter the first name of person ");
printf(" %d", i);
printf(": ");
fgets(first, 20, stdin);
for (k = 0; k < 20; k++)
{
firstName[i][k] = first[k];
}
i++;
}
i = 0;
while(i < choice)
{
printf("Enter the last name of person ");
printf(" %d", i);
printf(": ");
fgets(last, 20, stdin);
for (k = 0; k < 20; k++)
{
lastName[i][k] = last[k];
}
i++;
}
i = 0;
while(i < choice)
{
fputs("Enter the age of person ", stdout);
printf(" %d", i);
printf(": ");
scanf(" %d", &a);
fflush(stdout);
for (l = 0; l < 3; l++)
{
age[i][l] = a[l];
}
i++;
}
int sh;
while((sh = getchar()) != EOF && sh != '\n');
if (sh == EOF)
{
}
for (x = 0; x < choice; x++)
{
printf("First name ");
printf(": ");
printf("%s ", firstName[x]);
printf("\n");
printf("Last name ");
printf(": ");
printf("%s ", lastName[x]);
printf("\n");
printf("Age ");
printf(": ");
printf("%d ", &age[x]);
printf("\n");
}
return 0;
}
If you copy/paste this code it will run, but the age outputted will be incorrect. Can anyone tell me why this is? Thank you!
scanf(" %d", &a);
That should be:
scanf(" %d", &a[0]);
And the printf should be printf("%d", age[x][0]);
You want to read into the first element of the array, not the entire array. You want to print out the first element of the array, not the address of the array.
A better solution would probably be not to make age an array of 3 at all. Each person only has one age. The changes would be:
int age[choice];
int a;
scanf(" %d", &a);
age[choice] = a;
printf("%d ", age[x]);